21

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?

wageoghe
  • 27,390
  • 13
  • 88
  • 116
max
  • 213
  • 1
  • 2
  • 4

2 Answers2

24

Yes, you can do that. You can either configure the logger for that type to log to a specific target. Or you can configure the logger for that type to log to a target (such as a file), naming the file (automatically) based on the logger name.

See the NLog config file documentation here for some examples.

Also, see my post here for some config file tips.

Here is a very brief example of how you might configure two loggers: one for a specific type to be logged to an output file named for that type and one for all other loggers to log to a file based on the date.

<nlog>
  <targets> 
    <target name="f1" xsi:type="File" fileName="${logger}.txt" />
    <target name="f2" xsi:type="File" fileName="${shortdate}.txt" />
  </targets>
  <rules>
    <logger name="Name.Space.Class1" minlevel="Trace" writeTo="f1" />  
    <logger name="*" levels="Debug" writeTo="f2" />
  </rules>
</nlog>

If you want the logs for type Name.Space.Class1 to go to the "special" file (i.e. the one whose name is determined by the logger), then you can add "final" to the logger specfication like this:

<logger name="Name.Space.Class1" minlevel="Trace"final="true" />
Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116
0

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>
LIDEN
  • 156
  • 4
  • 1
    How do you explicitly write to a specific TARGET / LOGGER ? If a one method throws an error I want to write to **f1**, if another method (file operation) throws a error write to f2? – Jon Jun 16 '20 at 14:45
  • You'd need to open the specific logger that you want is whatever code you want to be able to write to that logger. For example, if your NLog.config rules section defined loggers with name="f1" and another with name="f2", then your code that wanted to write to f1 would do something like Logger f2Log = LogManager.GetLogger("f2"); – TheRealZing Jul 22 '21 at 20:31