Code Coverage by Clover.NET

Stopping/Starting NLog on runtime?

View: New views
4 Messages — Rating Filter:   Alert me  

Stopping/Starting NLog on runtime?

by FIB :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,

is there a way to start/stop the logging from an application?
I found the possibility to change the config-file and reload it after, but sadly that's no opinion for me, b/c of ClickOnce deployment, which means, that no file is allowed to be changed.

So my hope is that there is a flag i can set with my application to start/stop the logging. Is there something like that? Maybe prevent NLog from reading the config file or something like that?

best regards
Fiona

Re: Stopping/Starting NLog on runtime?

by Boersnoes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you set the autoReload option as such: <nlog autoReload="true" /> a change in the config file (through the config object, I wouldn't suggest direct file alterations) will have immediate result.
So you could for example remove a target from a rule so that it doesn't log anywhere.
Source: http://www.nlog-project.org/config.html (title Automatic Reconfiguration)
Hope that answers your question

Re: Stopping/Starting NLog on runtime?

by FIB :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey Boersnoes,

thanks for your answer, but this would change the config-file anyway, which isn't possible for me.
But after a deeper research in the sourcefiles I found a possibility:

if(chkLogger.Checked)
                        {
                                NLog.Config.SimpleConfigurator.ConfigureForFileLogging("Logfile.log", NLog.LogLevel.Trace);
                        }
                        else
                        {
                                NLog.Config.SimpleConfigurator.ConfigureForFileLogging("Logfile.log", NLog.LogLevel.Off);
                        }

This allows me to set the level during runtime and 'off' means no logging. Maybe I didn't make clear what I actually wanted ;)
The initial logging starts (or not) through the original config file, until the logging status is load and set.

Re: Stopping/Starting NLog on runtime?

by Gaborozzo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I used the LogManager.DisableLogging() and LogManager.EnableLogging() functions to turn off and on logging. It works fine.

Regards,
Gabor