How to avoid redundancy in NLog.Config?

I currently 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"/>

I would like to ensure that everything UPD CAX, etc. were collected at UPD.log, and the rest at ${shortdate}.log. I have achieved this. However, I think there is a lot of redundancy here because I need to add a template in both places.

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

+4
source share
1 answer

- , , . , , . , , , . :

<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>
+1

Source: https://habr.com/ru/post/1650417/


All Articles