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

标签 c# asp.net azure azure-application-insights

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

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

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

最佳答案

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

更多详情请引用此doc还有这个doc .

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

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

关于c# - 并非所有日志级别都记录在 Application Insights 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290524/

相关文章:

asp.net - 在 Azure 网站中将非 WWW 重定向到 WWW

visual-studio-2010 - 如何修复在 VS 2010 中运行 Azure 存储模拟器时出现的错误?

c# - DbMigrator.Update 导致数据库 'name' 已存在

c# - 从 C# 中的电子表格中提取时出现错误 "Specified cast is not valid"

c# - 使用引用 token 进行 ASPNETCore SignalR 身份验证

python - Azure 认知服务文本转语音 REST API 中的 requests.exceptions.ConnectTimeout 错误

c# - 发布到其他人的 Azure 订阅?

c# - 更改 api Controller 答案上的 json 字段名称

c# - 将 asp net 5 从 beta 升级到 beta 5 后,似乎无法构建类库项目

c# - 使用 ASP.NET Core Identity 在 Cookie 中保存 token