Application Insights 不捕获信息级日志记录
Application Insights does not capture information level logging
我有一个简单的 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>
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();
}
当 运行 在本地应用程序时,我可以按预期看到所有日志级别。
但针对同一请求的 Application Insights 仅捕获警告级别及更高级别的日志。下图来自 Application Insights 中的实时指标。
在 Application Insights 中查询日志时也是如此。
应用程序记录信息级别并被 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"
}
}
}
}
我有一个简单的 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>
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();
}
当 运行 在本地应用程序时,我可以按预期看到所有日志级别。
但针对同一请求的 Application Insights 仅捕获警告级别及更高级别的日志。下图来自 Application Insights 中的实时指标。
在 Application Insights 中查询日志时也是如此。
应用程序记录信息级别并被 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"
}
}
}
}