Rsetting Nlog variables

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

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}"/>
  <targets>
    <target name="afile" xsi:type="AsyncWrapper" queueLimit="100"
            overflowAction="Discard" />
    <target name="file" xsi:type="File" layout="${date} ${callsite} ${message}"
            fileName="${logFileName}.txt" />

Application.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <include file="GlobalNlog.config"/>
  <variable name="logFileName" value="NEWFILENAME"/>

 </nlog>


=================================

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?

Tim
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rsetting Nlog variables

Omnideth
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");

            LogManager.ReconfigExistingLoggers();



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
|  
Report Content as Inappropriate

Re: Rsetting Nlog variables

Francis1
In reply to this post by T Hayes
Hi,

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