Changing file Location at runTime

4 messages Options
Embed this post
Permalink
Nikson

Changing file Location at runTime

Reply Threaded More More options
Print post
Permalink
Hi,

I am writing an application which runs both in WinCE and Win32 devices ( same code).
I have created a config file for nLog mentioning all the properties(Target) and rules that I want to implement in my logging. But I have a problem: In win32 I have to write the Log in the Userdirectory\ApplicationData folder , whereas in winCE I have to write it in the Application Root folder.
I am unable to find a way by which I can change the Log file location programatically without changing the other configuration settings which I have already defined in the nLog.Config file. Let me know if it is possible. I am using C# for writing the app.

Thanks,
Niks
Jaroslaw Kowalski

Re: Changing file Location at runTime

Reply Threaded More More options
Print post
Permalink
You have a couple alteratives:

1. At startup, call GDC.Set("logdir", @"c:\path\to\my\log\directory");

And reference it in the configuration with

<target  ... fileName="${gdc:logdir}/logfile.txt" />

2. You can also directly modify configuration, by finding FileTarget and changing its FileName property. Make sure to do this on a configuration that has not been activated yet (i.e loaded but not assigned to LogManager.Configuration), otherwise behavior is undefined.

Nikson

Re: Changing file Location at runTime

Reply Threaded More More options
Print post
Permalink
thanks for your reply..

Can you please give some more information on the option "2"? I couldn't locate the proper infor on N-Log's web site.


Jay

Re: Changing file Location at runTime

Reply Threaded More More options
Print post
Permalink
I give my users a choice of:

Application Log Folder
Users Temporary Folder
Other Folder - (They use a folder browse dialog to set)

my NLog.config file has a target already created called applog.

At app startup, and also whenever the user changes the setting:

Dim config As LoggingConfiguration = LogManager.Configuration
        Dim standardTarget As FileTarget = TryCast(config.FindTargetByName("applog"), FileTarget)
        Select Case logDir
            Case "Application Log Folder"
                standardTarget.FileName = "${basedir}/logs/thunderlog.txt"
            Case "Users Temporary Folder"
                standardTarget.FileName = "${tempdir}/logs/thunderlog.txt"
            Case "Other Folder"
                standardTarget.FileName = logFileLocation.Replace("\", "/") & "/thunderlog.txt"
        End Select

        logger = LogManager.GetCurrentClassLogger()