azure - Application Insights 不捕获信息级别日志记录
问题描述
我有一个简单的 Asp.Net Core Web API 应用程序。对于这个例子,我在这里遵循了官方文档: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>
应用设置.json
{
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
},
"InstrumentationKey": "my-instrumentation-key",
"EnableAdaptiveSampling": false,
"EnablePerformanceCounterCollectionModule": false
},
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
启动.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiMonitoring", Version = "v1" });
});
services.AddApplicationInsightsTelemetry();
}
控制器.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();
}
在本地运行应用程序时,我可以按预期看到所有日志级别。
但是针对同一请求的 Application Insights 仅捕获警告级别的日志及更高级别的日志。下图来自 Application Insights 中的实时指标。
在 Application Insights 中查询日志时也是如此。
应用程序还需要什么来记录信息级别并被 Application Insights 捕获?
解决方案
你的配置不对。LogLevel 需要在 Logging 部分下指定
{
"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"
}
}
}
}
查看文档
推荐阅读
- rpgle - DLCOBJ 不发布文件
- docker - Google Compute Engine VM 在一段时间后停止
- c - Linux内核列表实现会导致UB吗?
- java - 在 Windows 上安装 Ejbca 时,“c:\ejbca\p12\truststore.jks”中缺少 JKS 信任库文件
- google-apps-script - 如果不满足第一个条件则不执行任何操作的 if 语句
- javascript - 如何访问“li”元素包含其子“a”的列表?
- sql - 检查列数据类型并在 SQL Server 中更改它
- ios - Ios中的错字反应本机无法识别
- ios - 在 iOS 14.2 上使用 VoIP 打开 CallKit 无法在终止状态下工作
- sql - TSQL:查询以最大大小的数据包分组文件