Kevin Orcutt
Kevin Orcutt

Reputation: 43

How to programmatically configure nLog targets

I am trying to figure out how to programatically configure a NLog FallbackGroup target that has two email type sub-targets. Based on some things that happen during the start of the application, I would like to override the "to" part of the two sub-targets of the FallbackGroup target.

Currently, the app has a NLog.config file that has these targets in it. But, the values are hard-coded in it. Any change will require a redeploy of the app... That's not what we want... What we really need is the ability to modify the "to" setting in the two targets in some logic called on startup.

Here's the NLog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
    autoReload="true"
    throwExceptions="false"
    internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<targets>
    <target name="logfile" xsi:type="File" fileName="BAM_logfile.txt" />
    <target xsi:type="FallbackGroup"
        name="email-error"
        returnToFirstOnSuccess="true">      
        <target xsi:type="Mail"
            name="mailserver1"
            to="[email protected]"
            from="[email protected]"
            subject="Exception Message from: ${processname} v:${assembly-version} on ${machinename}"
            smtpServer="smtp.acme.com"
            smtpPort="25"
            layout="${longdate}${newline}${windows-identity} running ${processname} v:${assembly-version} on ${machinename}${newline}At: ${callsite}${newline}Message: ${message}${newline}Exception:${newline}${exception:format=toString,Data:maxInnerExceptionLevel=10}${newline}" />
        <target xsi:type="Mail"
            name="mailserver2"
            to="[email protected]"
            from="[email protected]"
            subject="Exception Message from: ${processname} v:${assembly-version} on ${machinename}"
            smtpServer="mail.acme.com"
            smtpPort="25"
            layout="${longdate}${newline}${windows-identity} running ${processname} v:${assembly-version} on ${machinename}${newline}At: ${callsite}${newline}Message: ${message}${newline}Exception:${newline}${exception:format=toString,Data:maxInnerExceptionLevel=10}${newline}" />
    </target>
</targets>
<rules>
    <logger name="*" minlevel="Info" maxlevel="Warn" writeTo="logfile" />
    <logger name="*" level="Error" writeTo="email-error" />
</rules>
<extensions>
    <add assembly="NLog.MailKit"/>
</extensions>
</nlog>

So my question is... Is there a way of overriding the "to" part of the sub-targets in the NLog.config file programatically, or is it best to configure the entire FallbackGroup target on application start? A second and less obvious question is I'm looking for some examples of either solution... I haven't quite figured out the code to do either... :-(

Upvotes: 2

Views: 2437

Answers (1)

user47589
user47589

Reputation:

It works something like this:

Load the configuration from the appropriate XML file. This can be your app.config or a separate XML file. Then find the target by name, and cast it to the appropriate type. From there you can modify its properties however you see fit.

var xmlConfig = new XmlLoggingConfiguration("nlog.config");
var target = xmlConfig.FindTargetByName("mailserver1") as MailTarget;
target.To = "...";

You can do this with any target: load it by name, then cast it to the appropriate type. When you're done making changes, apply the configuration:

LogManager.Configuration = xmlConfig;

This should be done at startup, before you get any loggers. I think any loggers you get before applying these changes will not be affected.

Upvotes: 3

Related Questions