简体   繁体   English

如何使用C#在Nlog中动态禁用特定日志?

[英]how to disable the particular log, dynamically in Nlog using C#?

I tried to disable the particular log in nlog config and I added like this 我试图禁用nlog配置中的特定日志,并像这样添加

logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="t" enabled="false" 记录器名称=“ *” minlevel =“ Trace” maxlevel =“ Trace” writeTo =“ t” enabled =“ false”

is working fine. 工作正常。 I need to programmatically in c#. 我需要以编程方式在C#中。

You need something like this: 您需要这样的东西:

        // lookup for the rule in NLog config
        var rule = LogManager
            .Configuration
            .LoggingRules                
            .FirstOrDefault(_ => _.LoggerNamePattern == "*");

        // disable the rule
        if (rule != null)            
            rule.DisableLoggingForLevel(LogLevel.Trace);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM