Search code examples
c#loggingnlog

Add temporary additonal Memory Logger on nlog


I have nlog configuration working fine on a C# program. the nlog config is done via standalone file nlog.config In a specific part of the code I want however to create an additional memory logger target in order to save this specific part of logs in a buffer ( i will later attach this buffer to a specific object in the database).

To do so i try to add programmatically the logger and its config. I check that the logger is added and the rule is added, however nothing comes to the newly created logger. I think i miss something to tell nlog to actually take in consideration the new logger . once the target function is executed i want to stop to log this piece of code, stil use the other loggers, and do the same thing on a next item.

I add the memory logger with this lines of code:

                MemoryTarget memoryLogger = new MemoryTarget("memoryTarget");
                memoryLogger.Layout = "${message}";
                LogManager.Configuration.AddTarget(memoryLogger);
                LogManager.Configuration.AddRule(LogLevel.Trace, LogLevel.Fatal, memoryLogger);

below you see the nlog config before i add the new logger: config before

below the config after the new logger is added:

  • LogManager.Configuration.LoggingRules has a new element
  • LogManager.Configuration.AllTargets has a new element however in the details of the target i see:
  • IsInitialized to false
  • Loging config to null ( while i added the rule to LogManager.Configuration
  • the 'Logs' property which should contains all the lines logged is empty

I do not want to reinitialise all the nlog since it would reset my logfile, and i want a new log file at each process startup.

config after


Solution

  • Thank you @Rolf Kristensen,

    indeed,

    LogManager.ReconfigExistingLoggers();
    

    Permits to initialize the memory logger without resetting my other logger on File. By doing this the ConfigurationChanged/ConfigurationReloaded static events of LogManager are even not trigerred, and it works nicely! Great !

    Final code to activate and desactivate temporarily the logs in memory:

                    MemoryTarget memoryTarget = new MemoryTarget("memoryTarget");
                    memoryTarget.Layout = "${message}";
                    LogManager.Configuration.AddTarget(memoryTarget);
                    var memoryRule = new LoggingRule("memoryRule");
                    memoryRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Fatal);
                    memoryRule.Targets.Add(memoryTarget);
                    memoryRule.LoggerNamePattern = "*";
                    LogManager.Configuration.LoggingRules.Add(memoryRule);
                    LogManager.ReconfigExistingLoggers();
                    _log.Debug("this is dumped in memory");
                    foreach (string s in memoryTarget.Logs)
                    {
                        Console.Write("---------mem dumped: {0}", s);
                    }
                    LogManager.Configuration.RemoveTarget(memoryTarget.Name);
                    bool removed = LogManager.Configuration.LoggingRules.Remove(memoryRule);
                    LogManager.ReconfigExistingLoggers();
                    _log.Debug("no more in memory");