You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何NLog WriteToNil无法过滤指定日志记录器?

.NET MAUI中NLog过滤第三方日志失效问题

按照NLog.Targets.MauiLog的说明在.NET MAUI应用中配置NLog,基础日志功能正常,但尝试过滤Microsoft等第三方组件的冗余日志时,参考NLog Fluent配置API中「Build NLog config that restricts output from noisy logger」章节配置后,过滤始终不生效。

已尝试三种配置方式:

  • 初始配置:为Microsoft.AspNetCore.Components.*和Microsoft.EntityFrameworkCore.Database.Connection设置WriteToNil(NLog.LogLevel.Warn),内部日志显示仅过滤Warn及以上级别,但目标日志仍存在;
  • 修改级别:将上述配置改为WriteToNil(NLog.LogLevel.Debug),内部日志显示将过滤所有级别,但仍能看到目标日志;
  • 调整顺序:先配置全局文件和MauiLog输出,再设置WriteToNil规则,问题依旧。

初始配置代码

var logger = NLog.LogManager.Setup().RegisterMauiLog()
    .LoadConfiguration(builder =>
    {
        SimpleLayout layout = new SimpleLayout("${date:format=yyyy-MM-dd HH\\:mm\\:ss} ${uppercase:${level}} ${logger}: ${message} ${exception:format=tostring}");
        SimpleLayout filename = new SimpleLayout(Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "Log.log"));

        builder.ForLogger("Microsoft.AspNetCore.Components.*").WriteToNil(NLog.LogLevel.Warn);
        builder.ForLogger("Microsoft.EntityFrameworkCore.Database.Connection").WriteToNil(NLog.LogLevel.Warn);
        builder.ForLogger(NLog.LogLevel.Debug).WriteToFile(filename, layout, Encoding.UTF8, NLog.Targets.LineEndingMode.Default, true, false, 1048576, 3, 5);
        builder.ForLogger(NLog.LogLevel.Debug).WriteToMauiLog(layout);
        
        NLog.Common.InternalLogger.LogFile = Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "NLog-log.log");
        NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug;
        NLog.LogManager.GetLogger("Test").Info("Hello World");
    })
    .GetCurrentClassLogger();

初始配置对应内部日志

2023-12-19 19:26:09.4116 Debug logNamePattern: (Microsoft.AspNetCore.Components.:StartsWith) levels: [ Warn Error Fatal ] writeTo: [ ]
2023-12-19 19:26:09.4116 Debug logNamePattern: (Microsoft.EntityFrameworkCore.Database.Connection:Equals) levels: [ Warn Error Fatal ] writeTo: [ ]
2023-12-19 19:26:09.4116 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ File ]
2023-12-19 19:26:09.4116 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ MauiLog ]

修改级别后的配置代码

var logger = NLog.LogManager.Setup().RegisterMauiLog()
    .LoadConfiguration(builder =>
    {
        SimpleLayout layout = new SimpleLayout("${date:format=yyyy-MM-dd HH\\:mm\\:ss} ${uppercase:${level}} ${logger}: ${message} ${exception:format=tostring}");
        SimpleLayout filename = new SimpleLayout(Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "Log.log"));

        builder.ForLogger("Microsoft.AspNetCore.Components.*").WriteToNil(NLog.LogLevel.Debug);
        builder.ForLogger("Microsoft.EntityFrameworkCore.Database.Connection").WriteToNil(NLog.LogLevel.Debug);
        builder.ForLogger(NLog.LogLevel.Debug).WriteToFile(filename, layout, Encoding.UTF8, NLog.Targets.LineEndingMode.Default, true, false, 1048576, 3, 5);
        builder.ForLogger(NLog.LogLevel.Debug).WriteToMauiLog(layout);        
        
        NLog.Common.InternalLogger.LogFile = Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "NLog-log.log");
        NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug;
        NLog.LogManager.GetLogger("Test").Info("Hello World");
    })
    .GetCurrentClassLogger();

修改级别后对应内部日志

2023-12-19 19:27:59.8683 Debug logNamePattern: (Microsoft.AspNetCore.Components.:StartsWith) levels: [ Debug Info Warn Error Fatal ] writeTo: [ ]
2023-12-19 19:27:59.8683 Debug logNamePattern: (Microsoft.EntityFrameworkCore.Database.Connection:Equals) levels: [ Debug Info Warn Error Fatal ] writeTo: [ ]
2023-12-19 19:27:59.8683 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ File ]
2023-12-19 19:27:59.8683 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ MauiLog ]

调整顺序后的配置代码

var logger = NLog.LogManager.Setup().RegisterMauiLog()
    .LoadConfiguration(builder =>
    {
        SimpleLayout layout = new SimpleLayout("${date:format=yyyy-MM-dd HH\\:mm\\:ss} ${uppercase:${level}} ${logger}: ${message} ${exception:format=tostring}");
        SimpleLayout filename = new SimpleLayout(Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "Log.log"));

        builder.ForLogger(NLog.LogLevel.Debug).WriteToFile(filename, layout, Encoding.UTF8, NLog.Targets.LineEndingMode.Default, true, false, 1048576, 3, 5);
        builder.ForLogger(NLog.LogLevel.Debug).WriteToMauiLog(layout);
        builder.ForLogger("Microsoft.AspNetCore.Components.*").WriteToNil(NLog.LogLevel.Debug);
        builder.ForLogger("Microsoft.EntityFrameworkCore.Database.Connection").WriteToNil(NLog.LogLevel.Debug);
        
        NLog.Common.InternalLogger.LogFile = Path.Combine(FileSystem.Current.AppDataDirectory, "Logs", "NLog-log.log");
        NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug;
        NLog.LogManager.GetLogger("Test").Info("Hello World");
    })
    .GetCurrentClassLogger();

调整顺序后对应内部日志

2023-12-19 19:37:20.4908 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ File ]
2023-12-19 19:37:20.4908 Debug logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ MauiLog ]
2023-12-19 19:37:20.4908 Debug logNamePattern: (Microsoft.AspNetCore.Components.:StartsWith) levels: [ Debug Info Warn Error Fatal ] writeTo: [ ]
2023-12-19 19:37:20.4908 Debug logNamePattern: (Microsoft.EntityFrameworkCore.Database.Connection:Equals) levels: [ Debug Info Warn Error Fatal ] writeTo: [ ]

无效的最终结果

无论采用哪种配置,仍能看到目标日志记录器的输出,例如:

2023-12-19 19:26:12.5523 Debug Targets configured when LogLevel >= Debug for Logger: Microsoft.EntityFrameworkCore.Database.Connection
2023-12-19 19:26:12.5523 Debug Logger Microsoft.EntityFrameworkCore.Database.Connection [Debug] => File MauiLog
2023-12-19 19:26:12.5523 Debug Logger Microsoft.EntityFrameworkCore.Database.Connection [Info] => File MauiLog
2023-12-19 19:26:12.5523 Debug Logger Microsoft.EntityFrameworkCore.Database.Connection [Warn] => File MauiLog
2023-12-19 19:26:12.5523 Debug Logger Microsoft.EntityFrameworkCore.Database.Connection [Error] => File MauiLog
2023-12-19 19:26:12.5523 Debug Logger Microsoft.EntityFrameworkCore.Database.Connection [Fatal] => File MauiLog

目前无法确定问题所在,寻求解决方法。


内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 19:45:04