<?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="gmailUsername" value="test@bigvi.com" />
    <variable name="gmailPassword" 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="[${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="#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}" />

                
<!--                
          layout="#assembly-version: ${assembly-version}${newline}#basedir: ${basedir}${newline}#date: ${date}${newline}#document-uri: ${document-uri}${newline}#identity: ${identity}${newline}#install-context: ${install-context}${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}" />
          layout="asp-application: ${asp-application}\naspnet-application: ${aspnet-application}\naspnet-request: ${aspnet-request}\naspnet-session: ${aspnet-session}\naspnet-sessionid: ${aspnet-sessionid}\naspnet-user-authtype: ${aspnet-user-authtype}\naspnet-user-identity: ${aspnet-user-identity}\nasp-request: ${asp-request}\nasp-session: ${asp-session}\nassembly-version: ${assembly-version}\nbasedir: ${basedir}\ncallsite: ${callsite}\ncounter: ${counter}\ndate: ${date}\ndocument-uri: ${document-uri}\nenvironment: ${environment}\nevent-context: ${event-context}\nexception: ${exception}\nfile-contents: ${file-contents}\ngc: ${gc}\ngdc: ${gdc}\nguid: ${guid}\nidentity: ${identity}\ninstall-context: ${install-context}\nlevel: ${level}\nliteral: ${literal}\nlog4jxmlevent: ${log4jxmlevent}\nlogger: ${logger}\nlongdate: ${longdate}\nmachinename: ${machinename}\nmdc: ${mdc}\nmessage: ${message}\nndc: ${ndc}\nnewline: ${newline}\nnlogdir: ${nlogdir}\nperformancecounter: ${performancecounter}\nprocessid: ${processid}\nprocessinfo: ${processinfo}\nprocessname: ${processname}\nprocesstime: ${processtime}\nqpc: ${qpc}\nregistry: ${registry}\nshortdate: ${shortdate}\nsl-appinfo: ${sl-appinfo}\nspecialfolder: ${specialfolder}\nstacktrace: ${stacktrace}\ntempdir: ${tempdir}\nthreadid: ${threadid}\nthreadname: ${threadname}\nticks: ${ticks}\ntime: ${time}\nwindows-identity: ${windows-identity}\n" />
            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" />

<!--
        <logger name="idTheftLog" minlevel="Trace" writeTo="idTheftLogFile" />
-->
    </rules>
</nlog>
