简体   繁体   中英

Turn NLog on and off

I recently started using NLog to log data in a table in my database. 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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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