Search code examples
c#.netnlognlog-configuration

How to specify a default path for a File Target when a given event-property is not present?


I have a File Target as follow:

<target xsi:type="File" 
        name="f" 
        fileName="${basedir}/Logs/${event-properties:EmployeeID}.log" 
        ... 
/>

This file target logs the entries of each employee in a separate file based on the EmployeeID value, but some log entries don't have this EmployeeID property. How to log these entries to a default path ?

Is there syntax like this ?

fileName="${basedir}/Logs/${event-properties:EmployeeID || 'UnknownEmployee'}.log" 

Solution

  • The trick is to make use of the ambient layoutrenderer whenEmpty.

    Example:

    fileName="${basedir}/Logs/${event-properties:EmployeeID:whenEmpty=UnknownEmployee}.log"
    

    See also: https://github.com/nlog/nlog/wiki/WhenEmpty-Layout-Renderer