在.NET Framework 4.8中为Application Insights设置默认日志级别
.NET Framework 4.8中设置Application Insights默认日志级别
在.NET Framework 4.8中,无法直接像.NET Core那样通过appsettings.json配置日志级别,需要通过ApplicationInsights.config配置文件或者代码自定义过滤逻辑来实现相同效果,以下是具体方案:
方法1:通过ApplicationInsights.config配置全局日志级别
Application Insights提供了SeverityLevelFilterTelemetryProcessor处理器,可在配置文件中直接设置最低日志级别,过滤掉低于该级别的日志。
打开项目中的ApplicationInsights.config文件,在<TelemetryProcessors>节点下添加以下配置:
<TelemetryProcessors> <!-- 保留其他已有处理器配置 --> <Add Type="Microsoft.ApplicationInsights.Extensibility.SeverityLevelFilterTelemetryProcessor, Microsoft.ApplicationInsights"> <!-- 设置允许发送的最低日志级别,这里对应需求设为Warning --> <SeverityLevel>Warning</SeverityLevel> <!-- 可选:显式禁用低级别的日志,和SeverityLevel属性作用一致 --> <VerboseDisabled>true</VerboseDisabled> <InformationDisabled>true</InformationDisabled> <WarningDisabled>false</WarningDisabled> <ErrorDisabled>false</ErrorDisabled> <CriticalDisabled>false</CriticalDisabled> </Add> </TelemetryProcessors>
配置生效后,所有SeverityLevel低于Warning的Trace日志(比如Verbose、Information)都会被自动过滤,不会发送到Application Insights。
方法2:通过代码实现自定义日志级别过滤
如果需要动态调整日志级别,或者需要更灵活的过滤逻辑,可以通过自定义Telemetry处理器或直接在日志类中添加判断逻辑。
方案2.1:自定义Telemetry处理器(全局生效)
创建一个实现ITelemetryProcessor的过滤器类,全局过滤所有TelemetryClient的日志:
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.DataContracts; public class SeverityLevelFilterProcessor : ITelemetryProcessor { private readonly ITelemetryProcessor _nextProcessor; private readonly SeverityLevel _minimumAllowedLevel; public SeverityLevelFilterProcessor(ITelemetryProcessor nextProcessor, SeverityLevel minimumAllowedLevel) { _nextProcessor = nextProcessor; _minimumAllowedLevel = minimumAllowedLevel; } public void Process(ITelemetry telemetry) { // 仅过滤Trace类型的日志,其他类型(如请求、异常)不受影响 if (telemetry is TraceTelemetry traceTelemetry) { // 如果日志级别低于设置的最低级别,跳过发送 if (traceTelemetry.SeverityLevel < _minimumAllowedLevel) { return; } } // 传递符合条件的日志到下一个处理器 _nextProcessor.Process(telemetry); } }
然后在初始化TelemetryClient时注册这个处理器:
public class MyLogger { private static TelemetryClient _telemetryClient; public MyLogger() { _telemetryClient = new TelemetryClient(); // 注册过滤器,设置最低日志级别为Warning var config = TelemetryConfiguration.Active; config.TelemetryProcessorChainBuilder .Use(next => new SeverityLevelFilterProcessor(next, SeverityLevel.Warning)) .Build(); } public void Trace(string message) { _telemetryClient.TrackTrace(message, SeverityLevel.Verbose); } public void Information(string message) { _telemetryClient.TrackTrace(message, SeverityLevel.Information); } public void Warning(string message) { _telemetryClient.TrackTrace(message, SeverityLevel.Warning); } }
方案2.2:直接在日志类中添加级别判断(仅当前类生效)
如果只需要对MyLogger类的日志做过滤,可以直接在方法内添加级别判断:
public class MyLogger { private static TelemetryClient _telemetryClient; // 定义允许的最低日志级别 private readonly SeverityLevel _minLogLevel = SeverityLevel.Warning; public MyLogger() { _telemetryClient = new TelemetryClient(); } public void Trace(string message) { // 低于最低级别则不发送 if (SeverityLevel.Verbose < _minLogLevel) return; _telemetryClient.TrackTrace(message, SeverityLevel.Verbose); } public void Information(string message) { if (SeverityLevel.Information < _minLogLevel) return; _telemetryClient.TrackTrace(message, SeverityLevel.Information); } }
注意事项
- 确保已安装正确的Application Insights NuGet包,比如
Microsoft.ApplicationInsights或Microsoft.ApplicationInsights.WindowsServer,否则可能找不到相关处理器类。 - 配置文件方式修改后需要重启应用才能生效;代码方式则支持运行时动态调整级别。
内容的提问来源于stack exchange,提问作者Mohammad Taherian
相关产品推荐
相关产品推荐

