Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to log the info/errors using log4net.


---------------------------------------------
Thanks,
Rajeshwari

What I have tried:

have added this to assemblyinfo.cs file

[assembly: log4net.Config.XmlConfigurator(Watch = true)]


and Config file look like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="INFO" />     
      <appender-ref ref="RollingFileAppender" />
    </root>    
    
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <file value="c:\\LogFiles\\info.log" />      
      <staticLogFileName value="true" />
      <appendToFile value="true" />      
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="1MB" />     
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net> 
  
</configuration>


and calling the log method like below

public static class Logger
    {
        private static readonly log4net.ILog log =
            log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static void LogError(String message)
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Info("Application is working");
        }
    }



what might be the problem?
Posted
Updated 20-Feb-23 8:05am

Thanks for all the code samples. I created a console project to replicate and had no issues. The folder didn't exist to begin with on my machine it was created and log files wrote to it just fine.

I would suggest looking to see that your app has proper permissions to c:\logfiles directory.

Also another option would be to add

XML
<appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
   </appSettings>


To your .config and then see what debug messages are being generated by log4net.
 
Share this answer
 
Replace the line:
log4net.Config.XmlConfigurator.Configure();

by below:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900