NLog write log file only when event-property of a certain name is provided

32 Views Asked by At

I need to log into multiple files created dynamically using NLog. Let's say we want to log for each department into their respective file and the service messages into a default file. In order to try to achieve this I use

_logger.ForDebugEvent()
   .Message("Logging hard work...")
   .Property("Department", Dept.Name)
   .Log();

In the log config I configured the file name like this (NLog is configured from code):

FileName = "log-${event-properties:item=Department}.txt");
Layout = @"${date:format=yyyy.MM.dd. HH\:mm\:ss} [${level:uppercase=true}] ${message:when=${event-properties:item=Department}!=''",

This works, except that a 'log-.txt' file is also created (with no department name in the file name) with all events where the custom property is not set, plus all lines end with !=''. Clearly the 'when' condition is not properly used. I tried to deduct usage from this article: https://nlog-project.org/2011/04/20/exception-logging-enhancements.html

How can I make sure the target is written to only if the event property is set?

1

There are 1 best solutions below

1
Daniel On

I figured it out. The log config should look like this:

FileName = "log-${event-properties:item=Department}.txt";
Layout = @"${date:format=yyyy.MM.dd. HH\:mm\:ss} [${level:uppercase=true}] ${message}";

Then you shall add a filter:

        WhenNotEqualFilter fltDepartmentTarget = new()
        {
            Action = FilterResult.Log,
            CompareTo = string.Empty,
            Layout = "${event-properties:item=Department}"
        };

Then you need to add this filter to the rule writing the log:

        departmentRule.Filters.Add(fltDepartmentTarget);

This will basically allow logging only, when the Department property is set to something other than string.Empty in a log event. To log an event, you use

_logger.ForDebugEvent()
   .Message("Logging hard work...")
   .Property("Department", Dept.Name)
   .Log();

If you want to exclude these messages from other log files then you should add a WhenEqualFilter to those rules like this:

        WhenEqualFilter fltFileTarget = new()
        {
            Action = FilterResult.Log,
            CompareTo = string.Empty,
            Layout = "${event-properties:item=Department}"
        };
        fileRule.Filters.Add(fltFileTarget);