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

时间:2020-02-18 22:45:07

标签: c# asp.net azure-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时,记录的唯一内容是:

enter image description here

因此出于某种原因跳过了一些,仅记录了严重,警告和错误。我主要想使用LogInformation

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

1 个答案:

答案 0 :(得分:1)

如果要收集所有遥测数据,则不应在Warning中指定appsettings.json logLevel。 Warning日志级别将仅收集Warning / Error / Critical数据,但放弃Trace / Debug / {{1} }数据。

有关更多详细信息,请参阅此doc和此doc

请在Information中将应用程序见解的日志级别指定为Trace,如下所示:

appsettings.json
相关问题