codecompleting
codecompleting

Reputation: 9611

Need to modify this log4net to write to a file, with ability to write to many files

So I already have nhibernate working which uses log4net, and my web.config currently looks like:

<log4net debug="false">
  <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"/>
    </layout>
  </appender>
  <!-- Setup the root category, add the appenders and set the default priority -->
  <root>
    <priority value="INFO"/>
    <appender-ref ref="console"/>
  </root>
</log4net>

Now my current requirement is the ability to create a seperate log file for each major module that I have in my application.

Right now it seems to be just writing to console, but I need to add the ability to write to files.

What do I have to modify in my configuration to be able to do this?

I want to be able to, in my code, say I want to write to logfile "logfile_1", and if it doesn't exist, create it, otherwise just append to it.

Upvotes: 0

Views: 231

Answers (1)

Druegor
Druegor

Reputation: 389

Really simple, rinse and repeat as required for number of files you want, you can also associate namespaces with different files if you want them separated out that way but I find it easier just to use Log2Console to view the file all together.

<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value="k:\temp\log2console.log" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
</appender>

Don't forget to add the appender-ref to your root section.

Upvotes: 2

Related Questions