0

I am trying to create two different log files for two classes in one application. Everything is working fine except when program runs, two log files are created when one file is executed. So problem is two files are created by calling object for one appender.

following is the code :

<log4net>
      <appender name="Class1" type="log4net.Appender.FileAppender">
      <file value="c:\c1.log" />
      <appendToFile value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
    </appender>

    <appender name="Class2" type="log4net.Appender.FileAppender">
      <file value="c:\c2.log" />
      <appendToFile value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
    </appender>

    <logger name="LogClass1" additivity="false">
      <level value="ALL" />
      <appender-ref ref="Class1" />
    </logger> 

   <logger name="LogClass2" additivity="false">
      <level value="ALL" />          
      <appender-ref ref="Class2" />
    </logger>      
  </log4net>

and i am trying to call like :

log4net.ILog log = log4net.LogManager.GetLogger("LogClass1");

When Class1 gets called, it creates 2 files : "c1.log" and "c2.log" . Though c2.Log is 0 KB file and doesn't contain any data. How can i prevent this second file being made ?

  • Do you keep your `` configuration in your App.config and call `XmlConfigurator.Configure();`? Not sure, but this could trigger creating all referenced Appenders. – Filburt Mar 15 '17 at 10:17
  • 1
    log4net loads the appenders on when `Configure` is called, and part of the loading process is to create the log files. – stuartd Mar 15 '17 at 10:39
  • @Filburt , i have written like this : In `App.Config` : `
    ` and in `AssemblyInfo.cs` : `[assembly: log4net.Config.XmlConfigurator(Watch = true)]`
    – Ushma Mulwani Mar 15 '17 at 10:44
  • 1
    As it stands, you cannot stop the file being created. Log4net cannot know that you do not intend to use it, and correctly creates it for you. If you don't want it and you're not going to use it, why is it in the config anyway? – stuartd Mar 15 '17 at 11:07
  • @stuartd , i am using that file in other class file like i used it for the first file. The scenario is when one case from swtich case will run,`Class1` will get called and log will be generated using `LogClass1` logger and when other case will get called, `Class2` will be called and logs should be generated using `LogClass2` . Now, everything is working but when `Class1` is getting called, 2 files are getting created but i have made object for `LogClass1` logger only. – Ushma Mulwani Mar 15 '17 at 13:53
  • Possible duplicate of [log4net: Configure to ignore messages from a specific class](http://stackoverflow.com/questions/5504148/log4net-configure-to-ignore-messages-from-a-specific-class) – Offir Mar 27 '17 at 15:18
  • @UshmaMulwani: did you find a solution for this problem? – dhiraj suvarna Dec 22 '17 at 05:04

0 Answers0