Pablo
Pablo

Reputation: 29519

How to avoid redundancy in NLog.Config?

Currently I have the following configuration in NLog.Config:

<target name="upd" xsi:type="FilteringWrapper" condition="contains('${message}', 'UPD U40') 
        or contains('${message}', 'UPD CAX')
        or contains('${message}', 'UPD CAY')
        or contains('${message}', 'UPD CMVQA')
        or contains('${message}', 'UPD U68')
        or contains('${message}', 'UPD CBY')
        or contains('${message}', 'UPD CBX')
        or contains('${message}', 'UPD CUX')
        or contains('${message}', 'UPD CELL')
        or contains('${message}', 'UPD BPS')
        ">
  <target xsi:type="File" fileName="${basedir}/logs/UPD.log"
          layout="${longdate} - ${message}" />
</target>

<target name="other" xsi:type="FilteringWrapper" condition="not contains('${message}', 'UPD U40') 
        and not contains('${message}', 'UPD CAX')
        and not contains('${message}', 'UPD CAY')
        and not contains('${message}', 'UPD CMVQA')
        and not contains('${message}', 'UPD U68')
        and not contains('${message}', 'UPD CBY')
        and not contains('${message}', 'UPD CBX')
        and not contains('${message}', 'UPD CUX')
        and not contains('${message}', 'UPD CELL')
        and not contains('${message}', 'UPD BPS')
        ">
  <target xsi:type="File" fileName="${basedir}/logs/${shortdate}.log"
          layout="${longdate} - ${message}" />
</target>

...

<logger name="*" minlevel="Debug" writeTo="upd,other"/>

What I wanted to achieve is to have all UPD CAX etc. patterns to be collected in UPD.log and the rest in ${shortdate}.log. I achieved that. However, I think there is great redundancy here, because I have to add pattern to both places.

How can I simplify targets/rules to achieve the same result?

Upvotes: 4

Views: 105

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40788

The easiest way to simplify is probably by using variables. You could put your condition into a variable and then switch on whether the condition is true or not. Other parts like the file path and layout could also be variables if you want to configure them in one place. Here is a simple example:

<variable name="filterCondition" value="contains('${message}', 'UPD U40') 
    or contains('${message}', 'UPD CAX')
    or contains('${message}', 'UPD CAY')
    or contains('${message}', 'UPD CMVQA')
    or contains('${message}', 'UPD U68')
    or contains('${message}', 'UPD CBY')
    or contains('${message}', 'UPD CBX')
    or contains('${message}', 'UPD CUX')
    or contains('${message}', 'UPD CELL')
    or contains('${message}', 'UPD BPS')
    "/>
<variable name="logDir" value="${basedir}/logs" />
<variable name="logLayout" value="${longdate} - ${message}" />

<targets>
  <target name="upd" xsi:type="FilteringWrapper" condition="${filterCondition}">
    <target xsi:type="File" fileName="${logDir}/UPD.log"
      layout="${logLayout}" />
  </target>
  <target name="other" xsi:type="FilteringWrapper" condition="not (${filterCondition})">
    <target xsi:type="File" fileName="${logDir}/${shortdate}.log"
      layout="${logLayout}" />
  </target>
</targets>

Upvotes: 1

Related Questions