Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having NLog loggers with different configuration

Tags:

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?

like image 808
max Avatar asked Jul 17 '11 13:07

max


1 Answers

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" />
like image 125
wageoghe Avatar answered Sep 19 '22 14:09

wageoghe