当 appsettings.json 级别设置为 Inforamtion 时,Application Insights 捕获所有跟踪

问题描述 投票:0回答:1

我有一个非常简单的 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。 我需要做什么才能全局更改跟踪日志记录级别?

c# asp.net-web-api azure-application-insights
1个回答
0
投票
Microsoft-document

以及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");

enter image description hereappsettings.json:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ApplicationInsights": { "InstrumentationKey": "a2dec36e" } }

仅记录信息日志,未记录跟踪级别和所有:

enter image description here如果您获得其他信息,请使用更高的级别,只需记录该级别,这样就不会发生不需要的日志记录。

© www.soinside.com 2019 - 2024. All rights reserved.