oso*_*rio 3 azure azure-application-insights azure-web-app-service asp.net-core asp.net-core-webapi
我有一个简单的 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>
Run Code Online (Sandbox Code Playgroud)
应用程序设置.json
{
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
},
"InstrumentationKey": "my-instrumentation-key",
"EnableAdaptiveSampling": false,
"EnablePerformanceCounterCollectionModule": false
},
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)
启动.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiMonitoring", Version = "v1" });
});
services.AddApplicationInsightsTelemetry();
}
Run Code Online (Sandbox Code Playgroud)
控制器.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();
}
Run Code Online (Sandbox Code Playgroud)
在本地运行应用程序时,我可以按预期看到所有日志级别。
但同一请求的 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": "*"
}
Run Code Online (Sandbox Code Playgroud)
有效的配置是
{
"ApplicationInsights": {
"InstrumentationKey": "my-instrumentation-key",
"EnableAdaptiveSampling": false,
"EnablePerformanceCounterCollectionModule": false
},
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅文档
| 归档时间: |
|
| 查看次数: |
4540 次 |
| 最近记录: |