并非所有日志级别都记录在 Application Insights 中

Not all log levels are being logged in Application Insights

我有一个应用程序设置为使用 Application Insights,我正在尝试手动记录一些自定义信息。这是我的控制器:

public class MyController : Controller
{
    private ILogger<MyController> Logger { get; set; }

    public MyController(ILogger<MyController> logger)
    {            
        Logger = logger;
    }

    public IActionResult Index()
    {
        Logger.LogCritical("Test critical");
        Logger.LogError("Test error");
        Logger.LogWarning("Test warning");
        Logger.LogInformation("Test information");
        Logger.LogDebug("Test debug");
        Logger.LogTrace("Test trace");

        ...
    }

我的Startup.cs里有这个:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
    ...

}

这是我的 Program.cs:

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .UseStartup<Startup>()
            .Build();

在我的 appsettings.json 中:

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}

当我在我的 Azure 门户中查看 Application Insights 时,唯一记录的内容是:

所以它出于某种原因跳过了一些,只记录了 Critical、Warning 和 Error。我主要想使用 LogInformation.

我的日志记录设置或启动文件中是否需要更改某些内容?

如果要收集所有遥测数据,则不应在 appsettings.json 中指定 Warning logLevel。 Warning 日志级别只会收集 Warning / Error / Critical 数据,而放弃 Trace / Debug / Information ]数据。

详情请参考此doc and this doc

请在appsettings.json中将应用程序洞察的日志级别指定为Trace,如下所示:

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Trace"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}