Application Insights 不捕获信息级别日志记录 [英] Application Insights does not capture information level logging

本文介绍了Application Insights 不捕获信息级别日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Asp.Net Core Web API 应用程序.对于这个例子,我在这里遵循了官方文档:

但同一请求的 Application Insights 仅捕获警告级别及以上的日志.下图来自 Application Insights 中的实时指标.

在 Application Insights 中查询日志时也是如此.

应用程序还需要什么来记录信息级别并被 Application Insights 捕获?

解决方案

你的配置错误.LogLevel 需要在 Logging 部分下指定

{应用洞察":{日志级别":{默认":信息";},仪器密钥":我的仪器密钥",启用自适应采样":假,EnablePerformanceCounterCollectionModule":假},记录":{日志级别":{默认":信息";}},AllowedHosts":*"}

一个有效的配置应该是

{应用洞察":{仪器密钥":我的仪器密钥",启用自适应采样":假,EnablePerformanceCounterCollectionModule":假},记录":{日志级别":{默认":警告";},应用洞察":{日志级别":{默认":信息";}}}}

请参阅 文档

I have a simple Asp.Net Core Web API application. For this example I have followed the official documentation here: https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.18.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

appsettings.json

{
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Information"
    },
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "AllowedHosts": "*"
}

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiMonitoring", Version = "v1" });
            });

            services.AddApplicationInsightsTelemetry();
        }

Controller.cs

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var now = DateTime.Now;

            _logger.LogTrace("--- {time}: Logging LogTrace. Value {value}", now, 0);
            _logger.LogDebug("--- {time}: Logging LogDebug. Value {value}", now, 1);
            _logger.LogInformation("--- {time}: Logging LogInformation. Value {value}", now, 2);
            _logger.LogWarning("--- {time}: Logging LogWarning. Value {value}", now, 3);
            _logger.LogError("--- {time}: Logging LogError. Value {value}", now, 4);
            _logger.LogCritical("--- {time}: Logging LogCritical. Value {value}", now, 5);

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

When running the application locally I can see all the logs levels as expected.

But Application Insights for the same request is only capturing warning level logs and up. This following image if from Live Metrics in Application Insights.

The same goes when querying logs in Application Insights.

What else it is needed for an application to log information level and be captured by Application Insights?

解决方案

Your config is wrong. The LogLevel needs to be specified under the Logging section

{
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Information"
    },
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "AllowedHosts": "*"
}

A valid config would be

{
  "ApplicationInsights": {
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

See the docs

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

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