gpt4 book ai didi

azure - Application Insights 不捕获信息级别日志记录

转载 作者:行者123 更新时间:2023-12-02 06:02:11 26 4
gpt4 key购买 nike

我有一个简单的 Asp.Net Core Web API 应用程序。对于这个例子,我遵循了这里的官方文档:https://learn.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();
}

在本地运行应用程序时,我可以按预期看到所有日志级别。

enter image description here

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

enter image description here

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

enter image description here

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

最佳答案

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

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

有效的配置是

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

参见the docs

关于azure - Application Insights 不捕获信息级别日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69416109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com