Nlog: How to change writeTo at runtime?

Created on 26 Feb 2019  路  1Comment  路  Source: NLog/NLog

Is it possible to change writeTo value at runtime?

NLog version: 4.6.0-rc1

Platform: .Net 4.5

Current NLog config (xml or C#, if relevant)

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="console" xsi:type="Console" 
            layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}"/>
    <target name="file" xsi:type="File" fileName="${shortdate}-${windows-identity:domain=false}.txt"
            layout="${date:format=HH\:mm\:ss} - ${message}"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Error" writeTo="file" />
  </rules>
</nlog>
nlog-configuration question

Most helpful comment

yes, in C# / .NET

e.g.

```c#

var myNewTarget = new FileTarget();

var config = LogManager.Configuration;
config.LoggingRules[0].Targets.Clear();
config.LoggingRules[0].Targets.Add(myNewTarget);

//apply
LogManager.Configuration = config;

```

see also https://github.com/NLog/NLog/wiki/Configuration-API

>All comments

yes, in C# / .NET

e.g.

```c#

var myNewTarget = new FileTarget();

var config = LogManager.Configuration;
config.LoggingRules[0].Targets.Clear();
config.LoggingRules[0].Targets.Add(myNewTarget);

//apply
LogManager.Configuration = config;

```

see also https://github.com/NLog/NLog/wiki/Configuration-API

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smeegoan picture smeegoan  路  3Comments

npandrei picture npandrei  路  3Comments

sszost picture sszost  路  3Comments

Sam13 picture Sam13  路  3Comments

ericnewton76 picture ericnewton76  路  3Comments