Include files

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

Include files

rccnh
I'm still exploring, and tried to use a real simplistic "include file" and didn't have a problem. But just to be on the safe side I figured I'd ask the question.
My simple example had a regular NLog.config file with a set of targets and rules. I added into it an include pointing to a different config file, it too containing a target and rule.
When I ran my little sample application, it worked fine. It picked up the fact there was an additional target and rule and it looks like it just added into the collections it already had from the NLog.config.
 Is that the expected result?
 I was looking at using the include file to provide granuality (sp?) for each module and keeping them in separate config files. I understand I could do that within one big NLog.config file, but by breaking it out with the individual configs, each module and its developer would have control over their settings without effecting others.
 Maybe that was the original intention, but I wanted to confirm that assumption on my part.
 One additional question -- if NLog doesn't find the include file, it throws an exception. Is there a way to prevent that without having to change the code? Again, thinking that depending on what was happening, I could have a "base" NLog.config file that has an "include" for different modules. If I found I needed to debug a particular module, I could install the include file for it with its target and rule. Once any debugging wascompleted, delete the file.
Or maybe just overlay it with a blank config file? Which might get around the exception of not finding a file?
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Include files

Jarek Kowalski
Administrator
Yes, the targets and rules should merge correctly. To be exact, they are processed as if the contents of the included file have been inserted in the place of inclusion. The includes are processed top-down and the appropriate sections (<targets> and <rules> should appear to be merged in this order):

Assuming you have A.nlog:

<nlog>
    <targets>
        <target name="t1" />
    </targets>
    <rules>
        <logger name="l1" ... />
    </rules>
</nlog>

and B.nlog:

<nlog>
    <targets>
        <target name="t2" />
    </targets>
    <rules>
        <logger name="l3" ... />
    </rules>
</nlog>

and Application.exe.nlog:

<nlog>
     <include file="A.nlog" />
    <targets>
        <target name="t3" />
    </targets>
     <include file="B.nlog" />
    <rules>
        <logger name="l3" ... />
    </rules>
</nlog>

It is equivalent to this (note the order of t1,t2,t3 and l1,l2,l3):

<nlog>
    <targets>
        <target name="t1" />
        <target name="t3" />
        <target name="t2" />
    </targets>
    <rules>
        <logger name="l1" ... />
        <logger name="l2" ... />
        <logger name="l3" ... />
    </rules>
</nlog>

Answering your second question:

<include file="A.nlog" ignoreErrors="true" />

should do the job.
NLog Blog