1

I have a project in my solution that runs several different background jobs using Hangfire. I would like to be able to specify a different log file to be created using Log4Net based on the background job that is running.

For example; job 1 does validation checks across a database and job 2 synchronizes data from a 3rd party app into another database

I have several appenders specified in the config file but all log files get created when I run job 1 or job 2. I want a log file only to be created per job e.g. when I run Job 1 log file job1.xml gets created and not job2.xml and vice versa

Is there any way of specifying the appender only? In the below example I want the file c:\logging\testing\jobs3-%date{ddMMMyyyy-hhmmss}.xml to be created only not the others

XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger("xmlFile2");

=====config file========

    <appender name="file" type="log4net.Appender.RollingFileAppender">
      <file value="c:\logging\job1.log" />
      <appendToFile value="true" />
      <rollingStyle value="Once" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date - [%M] %level %logger - %message%newline" />
      </layout>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    </appender>
    <appender name="xmlFile" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="c:\logging\testing\job2-%date{ddMMMyyyy-hhmmss}.xml" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true" />
      </layout>
      <param name="Encoding" value="utf-8" />
    </appender>
    <appender name="xmlFile2" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="c:\logging\testing\jobs3-%date{ddMMMyyyy-hhmmss}.xml" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true" />
      </layout>
      <param name="Encoding" value="utf-8" />
    </appender>
  </log4net>
kurasa
  • 5,205
  • 9
  • 38
  • 53
  • think I have found the answer in this post https://stackoverflow.com/questions/1372435/configure-log4net-to-write-to-multiple-files?rq=1 – kurasa Oct 02 '18 at 01:28

0 Answers0