ILogger 不尊重 Application Insights 的日志级别 [英] ILogger Not Respecting Log Level for Application Insights

查看:32
本文介绍了ILogger 不尊重 Application Insights 的日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 ASP.NET Core 2.0 应用程序设置 Application Insights.在本地运行我的应用程序时,日志按预期显示在 Application Insights 中.但是,当部署到 Azure 应用服务时,在将日志发送到 Application Insights 中的请求"表时,异常"或跟踪"中没有显示任何日志.

I've been trying to set up Application Insights with an ASP.NET Core 2.0 application. While running my application locally, logs are showing up in Application Insights as expected. However, when deployed to an Azure App Service, while logs are being sent to the "requests" table in Application Insights, no logs are showing up in "exceptions" or "traces".

似乎解决此问题的唯一方法是将以下代码行添加到 Startup.Configure():

The only thing that seems to resolve this is adding the below line of code to Startup.Configure():

            loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);

上述解决方案是不可取的,因为我们希望根据环境配置不同的日志级别,因此首选基于配置的解决方案.另外,我的猜测是问题与配置有关,因为它在本地工作正常.Azure 中没有进行任何特殊配置.

The solution above is undesirable as we want to configure the log level differently by environment, so a configuration based solution would be preferred. Also, my guess is that the problem is configuration related as it works fine locally. There is no special configuration that was done in Azure.

这是整个 Startup.Configure():

Here is the entire Startup.Configure():

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();

    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);

}

我的Program.cs如下:

My Program.cs is as follows:

namespace TestLoggingApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

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

应用程序的 appsettings.json 文件如下(InstrumentationKey 替换为隐私):

The application's appsettings.json file is as follows (InstrumentationKey replaced for privacy):

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Debug"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Debug"
      }
    },
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "00000000-0000-0000-0000-000000000000"
  }
}

推荐答案

更新:此处提供了正确启用日志捕获的新说明.https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger

Update: New instructions are here for correctly enabling log capture. https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger

启用日志支持的正确方法是在 Configure 方法中使用 loggerFactory.AddApplicationInsights().当您从 Visual Studio 运行时,您不需要这一行,因为 VS 会在幕后为您完成.但是在 VS 外部运行时不起作用,因此请添加 loggerFactory.AddApplicationInsights 方法.https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging

The correct way of enabling logging support is by using loggerFactory.AddApplicationInsights() in Configure method. When you run from Visual Studio, you don't need this line because VS does it under the covers for you. But it won't work when ran from outside VS, so please add loggerFactory.AddApplicationInsights method. https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging

要根据环境获取不同的日志级别,请在条件语句中使用此行.类似于以下示例.

To get different log level based on environment, use this line inside condition statements. Something like the following example.

if(env.IsDeveleopment())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug);
}
else if(env.IsPreProduction())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Verbose);

}
else
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);

}

这篇关于ILogger 不尊重 Application Insights 的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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