Having NLog loggers with different configuration

Viewed 19259

In NLog is possible to create multiple loggers with different configuration?

I have a component that every time that is instanced must log all events to a different file that is related to the new instance.

Is this possible with NLog? If not, there are logging frameworks that do that?

2 Answers

My full example of NLog.config

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="false" internalLogFile="\\YOURSERVNAME\Logs\nlog-internal.log" internalLogLevel="Warn">
  <variable name="basedir" value="${basedir}/Logs"></variable>
  <targets>
    <default-target-parameters xsi:type="File" fileName="${var:basedir}/Trace.csv" archiveFileName="${var:basedir}/Archives/Trace/Trace.{##}.csv" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd" archiveEvery="Month" maxArchiveFiles="15" archiveAboveSize="10485760" keepFileOpen="false"/>
    <default-wrapper xsi:type="BufferingWrapper" bufferSize="500" flushTimeout="10000" />
    <target name="TraceLog" xsi:type="File">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
    <target name="ErrorLog" xsi:type="File" fileName="${var:basedir}/Errors.csv"  archiveFileName="${var:basedir}/Archives/Errors/Errors.{##}.csv" maxArchiveFiles="10">
      <layout xsi:type="CsvLayout" delimiter="Semicolon">
        <column name="Time" layout="${longdate}" />
        <column name="Callsite" layout="${callsite}" />
        <column name="Level" layout="${level}" />
        <column name="User" layout="${Identity}" />
        <column name="Message" layout="${message}" />
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="TraceLog" />
    <logger name="*" minlevel="Error" writeTo="ErrorLog" />
  </rules>
</nlog>
Related