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="" xmlns:xsi="">
      <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}"/>
      <logger name="MyLogger" minLevel="Trace" final="true" writeTo="MyLoggerTarget" />
      <logger name="*" minLevel="Trace" writeTo="debugger" />
      <logger name="*" minLevel="Debug" writeTo="logfile"/>

Below is how to instantiate `MyLogger`:

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

No comments: