Serilog异常消息在应用程序洞察中不可见 [英] Serilog exception message not visible in Application Insights

查看:11
本文介绍了Serilog异常消息在应用程序洞察中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.Net 5应用程序中,我有一个记录到应用程序洞察的Serilog配置。

"Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "MinimumLevel": {
      "Default": "Warning"
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "instrumentationKey": "xxx",
          "restrictedToMinimumLevel": "Warning",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ]
  }

出于测试目的,我按如下方式创建了测试API方法

public async Task<IActionResult> Test()
{
     logger.LogWarning("Warning message");
     logger.LogError(new CustomException("A custom exception message"), "Logger message");
     return Ok();
}

我希望在LogError方法中传递的消息应该显示在AI中。结果看起来是这样的:

如何使消息在日志中可见?我尝试通过将错误消息从日志传递到MessageTemplate来执行自定义遥测转换,但没有成功。

推荐答案

只需在Startup.cs中配置:

var config = TelemetryConfiguration.CreateDefault();
config.ConnectionString = configuration.GetSection("APPLICATIONINSIGHTS_CONNECTION_STRING").Value;

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .Enrich.WithProperty("ApplicationName", $"{applicationName} - {Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}")
    .Enrich.FromLogContext()
    .Enrich.WithCorrelationId()
    .WriteTo.Debug()
    .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.StaticFiles"))
    .WriteTo.ApplicationInsights(new TelemetryClient(config), TelemetryConverter.Traces)
    .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
    .CreateLogger();

这篇关于Serilog异常消息在应用程序洞察中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆