<?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="gmailUsername" value="david@blairbunch.com" />
  <variable name="gmailPassword" value="monkeyboy" />

  <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="[${longdate}] ${uppercase:${level}} [${machinename}] [${aspnet-request:servervariable=REMOTE_ADDR}] Serving request [${aspnet-request:servervariable=URL}] [${message}]" />

        <target xsi:type="File" 
            name="webLogFile" 
            fileName="${logdir}/webLog-${machinename}-${shortdate}.log"
            layout="[${longdate}] ${machinename} ${message}" />

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

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

      <!--
        <target xsi:type="File" name="file" fileName="c:/temp/logs/${shortdate}.log"
            layout="${longdate} ${machinename} - ${aspnet-request:servervariable=URL} ${uppercase:${level}} |${logger}|${threadid}|${basedir}|url-monkeybuf: ${message}" />

        <target xsi:type="File" name="file" fileName="c:/temp/logs/${shortdate}.log"
            layout="${longdate} ${aspnet-request:servervariable=URL} ${uppercase:${level}} url-monkeybuf: ${message}" />

        <target xsi:type="File" name="file" fileName="c:/temp/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} monkeyxbuf: ${message}" />
-->

        <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="*" minlevel="Trace" writeTo="file" />
        <logger name="*" minlevel="Trace" writeTo="allOutputs" />
-->

        <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" />
    </rules>
</nlog>
