0
votes

I want to have a different log for a specific part of a program, hence I created a new logging system in C#.NET using log4net. I have created a new node in app.config same as the one that is existing (default logs are also required.) The app.config looks something like this.

   <configuration>
    <configSections>

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

</configSections>

<log4net>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="FileAppender" />
    </root>

     <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="C:/Logs/DefaultLog.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <maximumFileSize value="10MB" />
        <maxSizeRollBackups value="-1" />
        <preserveLogFileNameExtension value="true" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
                   <conversionPattern value="%-7level   %date   %property{MemberName}   
                  %property{SourceLineNumber}   %message   %exception %newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG"/>
            <param name="LevelMax" value="ERROR"/>
        </filter>
    </appender>

  </log4net>
<customLog>
  <log4net>
  <root>
     <level value="ALL" />
     <appender-ref ref="PartialFileAppender" />
  </root>
  <appender name="PartialFileAppender" type="log4net.Appender.RollingFileAppender">
     <file type="log4net.Util.PatternString" value="C:/Logs/CustomLog.log" />
     <appendToFile value="true" />
     <rollingStyle value="Size" />
     <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
     <maximumFileSize value="10MB" />
     <maxSizeRollBackups value="-1" />
     <preserveLogFileNameExtension value="true" />
     <staticLogFileName value="true" />
     <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-7level   %date   %property{MemberName}   
        %property{SourceLineNumber}   %message   %exception %newline" />
     </layout>
     <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="ERROR"/>
     </filter>
  </appender>
  </log4net>
  </customLog>
  </configuration>

I have created this custom node which gets initialized like this.

 private readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

This logger configuration is getting done correctly after opening the app.config file.

if (File.Exists(appconfigpath))
     {
        xmlDocument.Load(File.OpenRead(appconfigpath));
        log4net.Repository.ILoggerRepository loggerRepository = log4net.LogManager.CreateRepository(
            Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
        log4net.Config.XmlConfigurator.Configure(loggerRepository, 
        (XmlElement)xmlDocument.SelectSingleNode("//partialfilelog/log4net"));
        xmlDocument = null;
     }

The new customlog file is getting created. But whenever I call the method to write a log in this file, the logs are getting created in the defaultlog file (These paths are provided in the appender inside value [value="C:/Logs/CustomLog.log"] like this.)

I am configuring both the logger from startup.cs file. Can anyone help me in understanding what is the problem here? Why my logs are getting generated in wrong file? What am I missing or doing wrong?

1

1 Answers

0
votes

Your config file still contains the default value.

Change <file type="log4net.Util.PatternString" value="C:/Logs/DefaultLog.log" /> to <file type="log4net.Util.PatternString" value="YourPathHere" />