<?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"
      autoReload="true"
      internalLogLevel="Trace"
      internalLogFile="c:\temp\logs\log.txt">

<!--
In order to generically assign a log directory for various environments, eg development, production (server) etc I had a need for the following:

set environment variable NLOG_BASE=D:\CommonLogs\${processname}\Logs
In NLog.config: create
<variable name="logdir" value="${whenEmpty:whenEmpty=${basedir:dir=Logs}:inner=${environment:NLOG_BASE}}"/>

In the target(s): fileName="${logdir}/debug.${shortdate}.txt"

However the environment renderer does not process the environment variable value as a Layout.
I have modified the EnvironmentLayoutRenderer code and attached it here.
This is a non-breaking change, and I recommended it be included in the next release.
    <variable name="logdir" value="${whenEmpty:whenEmpty=${environment:NLOG_BASE}:inner=C:/temp/logs/}"/>
-->

<!--
    <variable name="nlogbase" value="${environment:TEMP}"/>
    <variable name="nlogdefault" value="C:/temp/logs2/"/>
    <variable name="logdir" value="${whenEmpty:whenEmpty=${nlogbase}:inner=${nlogdefault}}"/>
    <variable name="logdir" value="c:/temp/logs/"/>
-->

    <variable name="logdir" value="${basedir}\..\..\logs"/>

    <variable name="emailUsername" value="test@bigvi.com" />
    <variable name="emailPassword" value="2aB.qCO5emIz" />

    <extensions>
        <!-- load NLog.Extended to enable ASP.NET-specific functionality -->
        <add assembly="NLog.Extended" />
    </extensions>

    <!-- define various log targets -->
    <targets>
        <!-- write logs to file -->
        <target xsi:type="File"
            name="serviceLogFile"
            fileName="${logdir}/serviceLog-${machinename}-${shortdate}.log">
            <layout xsi:type="CSVLayout">
                <delimiter>Tab</delimiter>
                <quoting>Nothing</quoting>

                <column name="date" layout="${longdate}" />
                <column name="machinename" layout="${machinename}" />
                <column name="message" layout="${message}" />
            </layout>
        </target>

<!--
        <target xsi:type="File"
            name="serviceLogFile"
            fileName="${logdir}/serviceLog-${machinename}-${shortdate}.log"
            layout="[${longdate}] ${uppercase:${level}} [${machinename}] [${aspnet-request:servervariable=REMOTE_ADDR}] Serving request [${aspnet-request:servervariable=URL}] [${message}]" />

        <target xsi:type="File"
            name="idTheftLogFile"
            fileName="${logdir}/idtheft-${machinename}-${shortdate}.log"
            layout="#logdir: ${logdir}${newline}#assembly-version: ${assembly-version}${newline}#basedir: ${basedir}${newline}#date: ${date}${newline}#identity: ${identity}${newline}#level: ${level}${newline}#longdate: ${longdate}${newline}#machinename: ${machinename}${newline}#message: ${message}${newline}#nlogdir: ${nlogdir}${newline}#processid: ${processid}${newline}#processname: ${processname}${newline}#processtime: ${processtime}${newline}#shortdate: ${shortdate}${newline}#specialfolder: ${specialfolder}${newline}#tempdir: ${tempdir}${newline}#threadid: ${threadid}${newline}#threadname: ${threadname}${newline}#ticks: ${ticks}${newline}#time: ${time}${newline}#windows-identity: ${windows-identity}${newline}" />
-->

        <target xsi:type="File"
            name="idTheftLogFile"
            fileName="${logdir}/idtheft-${machinename}-${shortdate}.log">
            <layout xsi:type="CSVLayout">
                <delimiter>Tab</delimiter>
                <quoting>Nothing</quoting>

                <column name="date" layout="${longdate}" />
                <column name="machinename" layout="${machinename}" />
                <column name="message" layout="${message}" />
            </layout>
        </target>

        <target xsi:type="File"
            name="webLogFile"
            fileName="${logdir}/webLog-${machinename}-${shortdate}.log">
            <layout xsi:type="CSVLayout">
              <delimiter>Tab</delimiter>
              <quoting>Nothing</quoting>

                <column name="date" layout="${longdate}" />
                <column name="machinename" layout="${machinename}" />
                <column name="message" layout="${message}" />
            </layout>
        </target>

        <!-- send mail through mail server -->
        <target xsi:type="Mail"
                name="mail"
                smtpServer="saffron.hmdnsgroup.com"
                smtpPort="587"
                enableSsl="true"
                smtpAuthentication="Basic"
                smtpUserName="${emailUsername}"
                smtpPassword="${emailPassword}"
                from="${emailUsername}"
                to="${emailUsername}"
                subject="Identity Theft Packet Request"
                addNewLines="true"
                />

        <target xsi:type="Mail"
                name="quartzEmail"
                smtpServer="saffron.hmdnsgroup.com"
                smtpPort="587"
                enableSsl="true"
                smtpAuthentication="Basic"
                smtpUserName="${emailUsername}"
                smtpPassword="${emailPassword}"
                from="${emailUsername}"
                to="${emailUsername}"
                subject="Charter Quartz Log"
                addNewLines="true"
                />

        <wrapper-target xsi:type="AspNetBufferingWrapper" name="bufferedWebLogFile">
            <wrapper-target xsi:type="PostFilteringWrapper">
                <compound-target xsi:type="SplitGroup">
                    <target-ref name="webLogFile" />
                </compound-target>

                <!-- during normal execution only log Info messages -->
                <defaultFilter>level >= LogLevel.Info</defaultFilter>

                <!-- if there is at least one error, log everything from trace level -->
                <when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
            </wrapper-target>
        </wrapper-target>

        <wrapper-target xsi:type="AspNetBufferingWrapper" name="bufferedServiceLogFile">
            <wrapper-target xsi:type="PostFilteringWrapper">
                <compound-target xsi:type="SplitGroup">
                    <target-ref name="serviceLogFile" />
                </compound-target>

                <!-- during normal execution only log Info messages -->
                <defaultFilter>level >= LogLevel.Info</defaultFilter>

                <!-- if there is at least one error, log everything from trace level -->
                <when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
            </wrapper-target>
        </wrapper-target>

        <wrapper-target xsi:type="AspNetBufferingWrapper" name="bufferedIdTheftLogFile">
            <wrapper-target xsi:type="PostFilteringWrapper">
                <compound-target xsi:type="SplitGroup">
                    <target-ref name="idTheftLogFile" />
                </compound-target>

                <!-- during normal execution only log Info messages -->
                <defaultFilter>level >= LogLevel.Info</defaultFilter>

                <!-- if there is at least one error, log everything from trace level -->
                <when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
            </wrapper-target>
        </wrapper-target>

        <wrapper-target xsi:type="AspNetBufferingWrapper" name="bufferedIdTheftEmailLogFile">
            <wrapper-target xsi:type="PostFilteringWrapper">
                <compound-target xsi:type="SplitGroup">
                    <target-ref name="mail" />
                </compound-target>

                <!-- during normal execution only log Info messages -->
                <defaultFilter>level >= LogLevel.Info</defaultFilter>

                <!-- if there is at least one error, log everything from trace level -->
                <when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
            </wrapper-target>
        </wrapper-target>
    </targets>

    <rules>
        <logger name="webLog" minlevel="Trace" writeTo="bufferedWebLogFile" />
        <logger name="serviceLog" minlevel="Trace" writeTo="bufferedServiceLogFile" />
        <logger name="idTheftLog" minlevel="Trace" writeTo="bufferedIdTheftLogFile" />
        <logger name="idTheftEmailLog" minlevel="Trace" writeTo="bufferedIdTheftEmailLogFile" />

        <logger name="quartzEmailLog" minlevel="Trace" writeTo="quartzEmail" />
    </rules>
</nlog>
