Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nlog log to another directory

By default Nlog logs to default application folder

        <target xsi:type="File" name="f" 
      fileName="${basedir}/logs/${shortdate}.log"
             layout="${longdate} ${uppercase:${level}} ${message} 
   ${exception:message=tostring}" />

Currently my application is in C directory and I want Nlog to log to D directory in specific folder. I read about

            fileName="${tempdir:folder=myapptmp}/sample.log"

and

       ${specialfolder:dir=String:file=String:folder=Enum}

Special folder it seems logs into MyDocuments, Pictures. So not much useful. Tempdir I am not sure about. Has anybody done this earlier or has any idea about it

My Nlog config

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- 
     See https://github.com/nlog/nlog/wiki/Configuration-file 
        for information on customizing logging rules and outputs.
        -->
      <targets>
     <!-- add your targets here -->


       <target name="File" xsi:type="File"  
     fileName="D:\Sushil\DwebLogging\log-${date:format=yyyy-MM-dd}.log"
        layout="${longdate} ${uppercase:${level}} ${message} ${exception: 
     format=tostring}" />
        <rules>
         <logger name="*" minlevel="Trace" writeTo="File" />
      </rules>
      </nlog> 
like image 848
Sushil Avatar asked Oct 16 '25 15:10

Sushil


1 Answers

Just specify the full path in the fileName attribute like so;

<target name="File" xsi:type="File"  fileName="D:\Logging\SomeFile-${date:format=yyyy-MM-dd}.log">

A full working sample is;

<?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">

  <targets>
    <target name="File" xsi:type="File"  fileName="D:\Logging\Sample-${date:format=yyyy-MM-dd}.csv">
      <layout xsi:type="CsvLayout">
        <column name="Index" layout="${counter}" />
        <column name="ThreadID" layout="${threadid}" />
        <column name="Time" layout="${longdate}" />
        <column name="Severity" layout="${level:uppercase=true}" />
        <column name="Location" layout="${callsite:className=False:fileName=True:includeSourcePath=False:methodName=False}" />
        <column name="Detail" layout="${message}" />
        <column name="Exception" layout="${exception:format=ToString}" />
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="File" />
  </rules>
</nlog>

If you're still not seeing your file, then it's possible that the process the application is running under doesn't have write access to the file (NLog will fail silently by default). This is most commonly found if you're app is a web app in IIS as the IIS process will need write access to the folder which it will not have by default.

like image 73
DiskJunky Avatar answered Oct 18 '25 05:10

DiskJunky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!