Add / remove logfiles during runtime in NLog

后端 未结 1 1521
野趣味
野趣味 2020-12-08 01:09

I\'m writing a small file conversion utility. Files get automatically converted when they are dropped into a directory.

I\'m using NLog for logging. Besides a centra

相关标签:
1条回答
  • 2020-12-08 01:17

    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!");
    
    0 讨论(0)
提交回复
热议问题