为什么NLog WriteToNil没有过滤掉指定的记录器?

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

我按照此处的说明在 .Net MAUI 应用程序中使用 NLog:https://github.com/NLog/NLog.Targets.MauiLog

日志记录设置良好,没有问题。但是,我想过滤掉一些冗长的第 3 方/微软组件。我按照此处的说明执行此操作:https://github.com/NLog/NLog/wiki/Fluent-Configuration-API(请参阅第

Build NLog config that restricts output from noisy logger
部分)。然而,无论我做什么,过滤器都不起作用:(

这是我的配置:

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();

此配置在 NLog 内部日志文件中生成以下输出:

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 ]

在我看来,这只是过滤级别上方警告我想要过滤掉的记录器(尽管 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.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();

此配置在 NLog 内部日志文件中生成以下输出:

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 ]

这看起来像是要过滤掉我试图摆脱的记录器的所有级别,更有希望,但可惜我仍然在我的日志中得到不需要的记录器:(例如:

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

最后我想我应该尝试以不同的顺序注册记录器:

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: [ ]

但是,我仍然在日志中收到不需要的记录器:(

此时我有点不知所措,不知道下一步该做什么。啊哈!

c# logging maui nlog
1个回答
0
投票

感谢您报告 NLog v5.2.7 在使用

WriteToNil
FinalMinLevel
Final
时无法报告预期的 LoggingRule 配置。

创建了一个 pull-request ,应该改进 NLog InternalLogger 的输出。因此您的初始配置:

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);        
    })
    .GetCurrentClassLogger();

将给出新的输出:

logNamePattern: (Microsoft.AspNetCore.Components.:StartsWith) levels: [ Trace Debug Info ] writeTo: [ ] finalMinLevel: Warn
logNamePattern: (Microsoft.EntityFrameworkCore.Database.Connection:Equals) levels: [ Trace Debug Info ] writeTo: [ ] finalMinLevel: Warn
logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ File ] finalMinLevel: Debug
logNamePattern: (:All) levels: [ Debug Info Warn Error Fatal ] writeTo: [ MauiLog ] finalMinLevel: Debug

使用

WriteToNil
创建的LoggingRules将显示具有较低严重性的LogLevels将写入空目标列表。

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