ILogger不尊重日志级别的应用程序见解 [英] ILogger Not Respecting Log Level for Application Insights

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

问题描述

我一直在尝试使用ASP.NET Core 2.0应用程序设置应用程序见解.在本地运行我的应用程序时,日志将按预期显示在Application Insights中.但是,当部署到Azure App Service时,虽然日志被发送到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/zh-cn/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不尊重日志级别的应用程序见解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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