Code Coverage by Clover.NET

Changing FileName programmatically

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

Changing FileName programmatically

by phlasher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
is it possible to configure NLog per NLog.config and then change the filename of a FileTarget programmatically on application launch?
I'm creating a c#-component and I want to let the user specify the LogPath...

I have to set minLevel AND maxLevel, and as far as I can tell, SimpleConfigurator doesn't provide methods to do that...

thanks in advance

Re: Changing FileName programmatically

by Boersnoes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You could always just alter the config file programattically, not?
This is maybe not as ellegant, but it does the job.

Re: Changing FileName programmatically

by Segway Rod :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I did it this way

            config = new LoggingConfiguration();

            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            fileTarget.Layout = "${longdate}|${level}|${callsite}|${message}";
            fileTarget.FileName = String.Format("${{basedir}}/{0}.log", filename);

            LoggingRule rule = new LoggingRule("*", LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(rule);

            LogManager.Configuration = config;

where filename is passed into the method as a parameter


Hope this is what you're looking for