Add / remove logfiles during runtime in NLog

The second post on this thread led me to the solution:
http://nlog-project.org/forum.html#nabble-td1685349

You have to get the current NLog configuration, make changes to this LoggingConfiguration object, then assign it back to LogManager.Configuration.

This is the code I used:

LoggingConfiguration config = LogManager.Configuration;

var logFile = new FileTarget();
config.AddTarget("file", logFile);

logFile.FileName = fileName + ".log";
logFile.Layout = "${date} | ${message}";

var rule = new LoggingRule("*", LogLevel.Info, logFile);
config.LoggingRules.Add(rule);

LogManager.Configuration = config;

logger.Info("File converted!");

Leave a Comment

tech