23

How can I add, edit, delete, enable, and disable loggers from code for NLog?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Stacker
  • 8,157
  • 18
  • 73
  • 135

3 Answers3

35

To add:

var logTarget = new ...
logTarget.Layout = "Your layout format here";
// e.g. "${logger}: ${message} ${exception:format=tostring}";

// specify what gets logged to the above target
var loggingRule = new LoggingRule("*", LogLevel.Debug, logTarget);

// add target and rule to configuration
LogManager.Configuration.AddTarget("targetName", logTarget);
LogManager.Configuration.LoggingRules.Add(loggingRule);
LogManager.Configuration.Reload();

Removal is done with

LogManager.Configuration.LoggingRules.Remove(loggingRule);
LogManager.Configuration.Reload();
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Does Reload reread the config ?if not how can i reread the config ? in case i added targets etc to the config directly ? – Stacker Sep 19 '11 at 13:25
  • @Stacker: Why would you want to add targets to the config directly? – Jon Sep 19 '11 at 13:28
  • the thing is i already have loggers that log the system , all i need is the ability to enable them disable them at runtime , maybe disable them by loglevel too , so i was thinking of editing the config file then make the logmanager reload it (reconfigure it from config file) – Stacker Sep 19 '11 at 13:29
  • 1
    @Stacker: That's a very roundabout way of doing so. You already have the API to enable and disable loggers: add or remove the rules that feed log events to them, as above. – Jon Sep 19 '11 at 13:31
  • well how do i save changes to config file then ? – Stacker Sep 19 '11 at 13:47
  • 1
    i like your approach, and i got how to reload the config ,its by adding the following : – Stacker Sep 19 '11 at 15:07
  • 1
    @Stacker: Glad to hear you solved it in the meantime. Cheers! – Jon Sep 19 '11 at 16:25
  • 6
    As I can see the easiest way to disable or enable all logs: LogManager.EnableLogging() and LogManager.DisableLogging() – Sasha Nov 15 '12 at 11:46
  • 1
    @Stacker According to the docs ( http://nlog-project.org/documentation/v2.0.1/html/M_NLog_Config_LoggingConfiguration_Reload.htm ) - `Reload()` is called by the LogManager when the config has changed, so calling `LogManager.Configuration.Reload();` is not going to reload the config. – Lasse Christiansen Jul 19 '13 at 11:15
  • @LasseChristiansen I read that differently. When LogManager detects that the config file has changed (due to autoReload being turned on) it calls Reload as its way of reloading the configuration. Reload does not actually change the configuration, unless you do LogManager.Configuration = LogManager.Configuration.Reload(); – Xtros Mar 01 '19 at 16:44
14

I know this is an old answer but I wanted give feedback for anyone looking to make modifications to their targets and logging rules programmatically that Configuration.Reload() doesn't work.

To update existing targets programmatically you need to use the ReconfigExistingLoggers method:

var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
target.FileName = "${logDirectory}/file2.txt";
LogManager.ReconfigExistingLoggers();

An example that adds and removes logging rules on the fly:

if (VerboseLogging && !LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Add(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}
else if (!VerboseLogging && LogManager.Configuration.LoggingRules.Contains(VerboseLoggingRule))
{
    LogManager.Configuration.LoggingRules.Remove(VerboseLoggingRule);
    LogManager.ReconfigExistingLoggers();
}

As written in docs:

Loops through all loggers previously returned by GetLogger. and recalculates their target and filter list. Useful after modifying the configuration programmatically to ensure that all loggers have been properly configured.

This answer and sample comes from Tony's answer in:

Update NLog target filename at runtime

Community
  • 1
  • 1
Neil Bostrom
  • 2,254
  • 21
  • 24
  • 2
    Thank you! I spent several hours trying to find out why my config changes weren't propagating to my Log object. NLog has gotten way to convoluted IMHO. – SteveCinq Apr 14 '19 at 07:07
2

NLog 4.6.7 makes it possible to assign Layout-variables to LoggingRule-levels, and change these Layout-variables at runtime.

<nlog>
    <variable name="myLevel" value="Warn" />
    <rules>
      <logger minLevel="${var:myLevel}" />
    </rules>
</nlog>

Then you can do this in code:

LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();

See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70