简体   繁体   English

打开和关闭 NLog

[英]Turn NLog on and off

I recently started using NLog to log data in a table in my database.我最近开始使用 NLog 在我的数据库的表中记录数据。 It seems to work perfectly fine.它似乎工作得很好。

Now I wanted to add a toggle in my app so I can quickly enable or disable all the Logging.现在我想在我的应用程序中添加一个切换按钮,以便我可以快速启用或禁用所有日志记录。 Is there a nice and easy way to do that with NLog?使用 NLog 有没有一种很好且简单的方法来做到这一点?

The only way I have found so far is having autoReload set to true and then letting a button comment out or back the NLog.config file.到目前为止,我发现的唯一方法是将 autoReload 设置为 true,然后让按钮注释掉或返回 NLog.config 文件。 That feels very shady and cumbersome though and I was wondering if there is a better way to do it!但这感觉非常阴暗和麻烦,我想知道是否有更好的方法来做到这一点!

You could use你可以用

LogManager.DisableLogging();
LogManager.EnableLogging();

Another option:另外一个选项:

LogManager.Factory.GlobalThreshold = LogLevel.Off; 

Which makes it possible to globally only log fatal errors etc.这使得全局仅记录致命错误等成为可能。

Last, but not least, this could be configured on rule level, see LogManager.Configuration.LoggingRules then.最后但并非最不重要的是,这可以在规则级别进行配置,然后请参阅LogManager.Configuration.LoggingRules

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

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