1
votes

I have the following NLog configuration 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">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
    -->
  <targets>
    <target
      name="logfile"
      xsi:type="File"
      fileName="log.txt"
      archiveFileName="log.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
    />
    <target
      name="byteArrayLogfile"
      xsi:type="File"
      fileName="log.ba.txt"
      archiveFileName="log.ba.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
      />
    <target 
      name="console" 
      xsi:type="Console" />
  </targets>


  <rules>
    <logger name="StackTraceLogger" minlevel="Warn" maxlevel="Error" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}" final="true"/>
    <logger name="ByteArrayLogger" minlevel="Debug" writeTo="byteArrayLogfile" layout="${longdate}|${threadid}|${level:uppercase=true}${newline}${message}" final="true"/>
    <logger name="Ctrack.DMT.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="Ctrack.DMT.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="ServerForm.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="ServerForm.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
  </rules>
</nlog>

The aim is to:

  1. send anything from the class library or the winform at trace level + to the console
  2. send everything from the class library or the winform at debug level + to the logfile
  3. send anything from the ByteArrayLogger to byteArrayLogfile
  4. send everything from the ExceptionLogger to the logfile, using a slightly different layout

Every class has the following up the top:

private static Logger logger = LogManager.GetCurrentClassLogger();
private static Logger loggerEL = LogManager.GetLogger("ExceptionLogger");
private static Logger loggerBA = LogManager.GetLogger("ByteArrayLogger");

Steps to log exception

string message = "Exception while trying to save " + Name + " to the database.";
logger.Error("{0}|{1}", MethodName, message);
loggerEL.Error(ex, "{0}|{1}", MethodName, message);

Stacktraces may be moved out to a separate file in the near future

Things that aren't working

  • No thread id. Interestingly no missing | separator
  • No line numbers. Interestingly no missing | separator
  • Exceptions aren't in the log file. They should be in the file but using a different layout
  • Console is showing DEBUG level items

In addition, Intellisense is highlighting layout and saying The 'layout' attribute is not declared as a warning.

1
Accepting Julians answer. Will post a new question with updated changes and issues - Hecatonchires

1 Answers

0
votes

In addition, Intellisense is hilighting layout and saying The 'layout' attribute is not declared as a warning.

In this case that's also the error. The layout should be on the <target> and not on the <logger>

So

 <targets>
    <target
      name="logfile"
      xsi:type="File"
      fileName="log.txt"
      archiveFileName="log.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
 layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}"
    />
   ...

and

  <rules>
    <logger name="StackTraceLogger" minlevel="Warn" 
            maxlevel="Error" writeTo="logfile" final="true" />
     ...
 </rules>