Rsetting Nlog variables

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view

Rsetting Nlog variables

T Hayes
Suppose I include a Global Nlog config file in my application config file. like so:

Global Nlog:

  <variable name="logFileName" value="${Logger}"/>
    <target name="afile" xsi:type="AsyncWrapper" queueLimit="100"
            overflowAction="Discard" />
    <target name="file" xsi:type="File" layout="${date} ${callsite} ${message}"
            fileName="${logFileName}.txt" />


<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns=""
   <include file="GlobalNlog.config"/>
  <variable name="logFileName" value="NEWFILENAME"/>



Is there a way to "redefine" the variable logFileName so that the Application Nlog config file can route the logs to a different filename if they do not want to use what is defined in the Global config file?

Reply | Threaded
Open this post in threaded view

Re: Rsetting Nlog variables

Hey man, it sounds like you need to know about pulling the files in to objects.  Here's a snippet that should kick you in the right direction:

            var config = LogManager.Configuration;

            var dbTarget = (DatabaseTarget)config.FindTargetByName("log");
            dbTarget.CommandText = string.Format("new update command");


So basically, you can ask the LogManager (using NLog statement needed) to pull the xml layout objects into c# objects.  Modify them as shown, my program used a dynamic insert statement for the database as opposed to different log file locations, and then after everything is set do the last call.  You can then get the object again and see the changes are in fact made, though not represented in the xml file.  The changes will revert to the original xml file when the program is finished.

Hope that helps.
Reply | Threaded
Open this post in threaded view

Re: Rsetting Nlog variables

In reply to this post by T Hayes

Did you find a way to do this? I'm running into the same issue.