My Technical Notes

Friday, 7 November 2014

NLog: Create a named Logger

In Nlog, it is possible to have a specially named logger which works independently of all the other loggers. Below is the `<nlog>` element that has a logger named `MyLogger`. Note that it is marked as `final`, therefore all the loggers below it will not process the log message.


<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
      <target name="MyLoggerTarget" xsi:type="File" fileName="c:\temp\MyLog.txt" layout="${longdate} ${callsite} ${level} ${message}" />
      <target name="debugger" xsi:type="Debugger" layout="${longdate} ${callsite} ${level} ${message}" />
      <target name="logfile" xsi:type="File" fileName="c:\temp\scu.log.txt" layout="${longdate} ${callsite} ${level} ${message}"/>
    </targets>
    <rules>
      <logger name="MyLogger" minLevel="Trace" final="true" writeTo="MyLoggerTarget" />
      <logger name="*" minLevel="Trace" writeTo="debugger" />
      <logger name="*" minLevel="Debug" writeTo="logfile"/>
    </rules>
</nlog>

Below is how to instantiate `MyLogger`:


private static Logger MyLogger = LogManager.GetLogger("MyLogger");

No comments: