Vladimir
Vladimir

Reputation: 103

Filtering targets in NLog

I have a logger that configured to multiple targets. Can I filter targets with some conditions?

<logger name="actionsLogger" minlevel="Info" writeTo="fileTarget,rmqTarget1,rmqTarget2" />

I need to always write actions to fileTarget and in some cases to rmqTarget1 and rmqTarget2. Maybe solution is to create multiple loggers with their own targets. But I can't change source code of the project and recompile.

Upvotes: 2

Views: 2635

Answers (2)

Stijn
Stijn

Reputation: 2060

Take a look at Conditions on the NLog wiki.

Conditions are filter expressions used with the when filter. They consist of one or more tests. They are used in the when filter to determine if an action will be taken.

You can do this with conditions:

<logger name="actionsLogger" minlevel="Info" writeTo="fileTarget" />
<logger name="actionsLogger" minlevel="Info" writeTo="rabbitMQTarget">
   <filters defaultAction="Log">
        <when condition="'${event-property:item=Status}'=='Success'" action="Ignore" />
   </filters>
</logger>

Upvotes: 0

Vladimir
Vladimir

Reputation: 103

Thanks for the reply. I have found the solution. FilteringWrapper - https://github.com/NLog/NLog/wiki/FilteringWrapper-target. It applies filter for target instead of filter for logger.

<target
    xsi:type="FilteringWrapper"
    condition="'${event-context:item=Status}'=='Success'"
    name="rabbitMQFilteringTarget">
    <target
        xsi:type="RabbitMQ"
        name="rabbitMQTarget"
...
    </target>
</target>

Upvotes: 3

Related Questions