KeyBored
KeyBored

Reputation: 621

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" 

Upvotes: 1

Views: 60

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19847

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

Upvotes: 3

Related Questions