0

My Project is in C# (Windows Form) .net3.5

In my code there are multiple events and in each event multiple searches are running.

I have to create log file for each event and multiple searches can access & write this single log file.

The problem is that I don't know how to use log4net for creating multiple logs with dynamic(set at run time) names.

How to set there location(Path)

I explorer internet regarding my problem but haven't found any help which address this type of issues.

any idea pls

Community
  • 1
  • 1
huda
  • 4,107
  • 2
  • 21
  • 24

1 Answers1

0

log4net is generally used to have mutiple sources and appenders. To generate a defined source, ask LogManager to have a named log, generally we use a type to identify a log, but feel free to use a string if you want:

    var myLog = LogManager.GetLogger("NAME");
....
    var myLog = LogManager.GetLogger(GetType());

In the first case logger is identified by "NAME", in the second by the type name with namespaces. Then you can configure the appenders, for all logs, for a subset of them, with filters and so on. Let's see an example:

<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

  </configSections>
  <log4net>

<appender name="MYFILE" type="log4net.Appender.RollingFileAppender">
  <file value=".\logs\myname.log"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="10"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level (%thread) %logger - %message%newline"/>
  </layout>
</appender>

<root>
  <level value="INFO"/>
  <!-- all logger append to these appenders />
   <appender-ref ref="xxxxxx"/>

</root>
<logger name="NAME">
  <level value="INFO"/>

  <appender-ref ref="MYFILE"/>
</logger>
<!-- add other appender here -->

Don't forget to call at your application start XmlConfigurator.Configure();

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115