NLog on is popular logging component for .NET applications. I am using it in one project where I built multi-threaded server that is running similar threads with different configuration. As those thread are independent services I needed way how to configure NLog to create different log file for each thread. Here is my solution.
var target = new FileTarget();
target.Name = InstanceName;
target.FileName = LogsFolder + "/${shortdate}.log";
target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
var config = new LoggingConfiguration();
config.AddTarget(this.Name, target);
var rule = new LoggingRule("*", LogLevel.Info, target);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
_logger = LogManager.GetLogger(InstanceName);
_logger.Info("Logger is initialized");
This way I got all logging done on code level and as there is arbitrarily small probability that logging ever changes I am very sure that this seemingly temporary solution will live with project for long time.
View Comments (2)
Can this be done for different controller or routes. Can all logs for a request be grouped?
You can do it with different routes. In logs you can use request based correlation ID to later filter out requests of your interest. Take a look at Trace Activity Id Layout Renderer here: https://github.com/NLog/NLog/wiki/Trace-Activity-Id-Layout-Renderer