我有一个非常简单的 Asp .Net API 项目,我试图在其中添加一些跟踪日志记录。 我需要一些有关该日志记录的 Application Insights 配置的帮助。 现在它在 appsettings.json 中捕获所有跟踪,我已将 Default 级别设置为 Information。
我的 Program.cs
中有此代码services.AddApplicationInsightsTelemetry();
随后:
Trace.Listeners.Add(new Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener());
以及我的 appsettings.json 中的以下内容:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning"
}
}
我的 .csproj 文件中有这两个包:
<PackageReference Include="Microsoft.ApplicationInsights.TraceListener" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
我希望只有级别为“信息”或以上的跟踪才会被记录到 Application Insights,但是我也看到所有内容都被记录到 Application Insights。 我需要做什么才能全局更改跟踪日志记录级别?
以及SO-thread's答案: 我在program.cs中使用了以下代码:
builder.Services.AddSingleton(typeof(ITelemetryChannel), new ServerTelemetryChannel());
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddServiceProfiler();
并在 csproj 中添加以下行:
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.4.0" />
在控制器中添加了记录器代码:
_logger.LogInformation("Hello Rithwik");
_logger.LogTrace("Hello");
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"InstrumentationKey": "a2dec36e"
}
}
仅记录信息日志,未记录跟踪级别和所有:
如果您获得其他信息,请使用更高的级别,只需记录该级别,这样就不会发生不需要的日志记录。