c# - 应用程序洞察中自定义对象的属性在哪里

标签 c# azure azure-application-insights ilogger

我有一个像这样的自定义对象。

 public class Propertey
{
    public string Key { get; set; }
    public string Value { get; set; }
}
public class MonitoringEvent 
{
    public string AppName { get; set; }
    public string Service { get; set; }
    public string Fuction { get; set; }
    public string CorrelationId { get; set; }
    public List<Propertey> Properties { get; set; }
    public string EventName { get; set; }
    public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
}

这个对象是从系统外部填充的,在我的函数应用程序中,我试图将其记录到 App Insights 中。

     [FunctionName("EventMonitoring")]
    public async Task Run([ServiceBusTrigger(
            "cta-event-monitoring",
            "monitoring",
            Connection = "ServiceBusConnectionString",
            IsSessionsEnabled = false)]string mySbMsg, ILogger log)
    {
        try
        {
            MonitoringEvent me = JsonConvert.DeserializeObject<MonitoringEvent>(mySbMsg);
           
            log.LogInformation("MonitoringEvent", me);
        }
        catch (Exception ex)
        {

            log.LogError("MonitoringEventError",ex);
        }
    }

当我看到应用程序见解时,我在应用程序见解中看不到“AppName”“Service”等属性。我在哪里可以找到它们?稍后我也希望能够查询它们。但我在应用程序洞察中看到的都是这样的

enter image description here

最佳答案

您需要使用log message template登录。消息模板可以包含为其提供参数的占位符。使用占位符的名称。在您的情况下,这意味着您需要添加命名模板位置

log.LogInformation("MonitoringEvent {Event}", me);

这将在应用程序洞察的跟踪中创建自定义属性“事件”。

在当前代码中,您没有提供占位符,因此参数无法放置在某处并且会被忽略。

此外,请注意应用程序洞察可能会在任何 object 参数上使用 .ToString(),因此最好的选择是仅使用 mySbMsg code> 作为参数。

关于c# - 应用程序洞察中自定义对象的属性在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67679736/

相关文章:

c# - 如何使用 .NET Core 创建仅托盘的 Windows 服务 Controller ?

azure - 有没有办法为 DevFabric 使用不同的 SSL 证书?

azure - 如何更深入地了解我的用户以及他们在 Azure 中访问的端点?

azure - 通过 Bicep 为 StaticWebApp 创建 AppInsights 实例

c# - CScore 输出大于一个字节的 PCM

c# - sql server 的连接字符串中的 Enlist=false 是什么意思?

c# - 为什么 Interlocked.Add() 方法必须返回一个值?

azure - 在 JWT token 中包含 AAD 组名称

azure - 如何使用 CLI 创建 Azure 备份策略?

Azure Application Insights 不跟踪 SQL 查询