I would like to define 2 targets (typically file target on Devops and UDP locally) in nlog and choose them dynamically based on location. Motivation for this is to use different targets when running locally and running on Devops.
There are a number of articles on how to do this using C# code, but I was wondering if it is possible to do this directly in the config file.
In Pseudo Code I am looking for something like this in my rules section:
<rules>
if(Env==DEVOPS)
<logger name="*" minlevel="Info" writeTo="file" />
else
<logger name="*" minlevel="Trace" writeTo="udp" />
end
</rules>
I suggest that you use the method described here:
<nlog>
<variable name="myFileLevel" value="Off" />
<variable name="myUdpLevel" value="Off" />
<rules>
<logger name="*" minLevel="${var:myFileLevel}" writeTo="file" />
<logger name="*" minLevel="${var:myUdpLevel}" writeTo="udp" />
</rules>
</nlog>
And then do this at runtime:
if (DevOps)
{
LogManager.Configuration.Variables["myFileLevel"] = "Debug";
}
else
{
LogManager.Configuration.Variables["myUdpLevel"] = "Trace";
}
LogManager.ReconfigExistingLoggers();
See also: https://github.com/nlog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration