Search code examples
c#log4netclass-library

log4net to a class library


I have a class library project attached to internet explorer (add-on for explorer), I want to log this class library with log4net, I added a App.config file to the project:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <appSettings>
    <add key="log4net.Config" value="log4net.config"/>
    <add key="log4net.Config.Watch" value="True"/>
  </appSettings>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingFileAppenderSize" type="log4net.Appender.RollingFileAppender">
      <file value="FromLog4netLon.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <encoding value="utf-8" />
      <maximumFileSize value="10MB" />
      <maxSizeRollBackups value="5" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="FATAL" />
      <level value="WARN" />
      <level value="INFO" />
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppenderSize" />
    </root>
  </log4net>
</configuration>

And under AssemblysInfo.cs I added [assembly: log4net.Config.XmlConfigurator(Watch = true)]

And inside my project I added

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

And I try to write like:

log.Info("test log");

I also try to add this:

FileInfo fi = new FileInfo(@"D:\IExtonsionsTests\1\bin\Debug\ClassLibrary.dll.config");
log4net.Config.XmlConfigurator.Configure(fi);

But it doesn't write log file, what am I missing?


Solution

  • Make sure your file value is correct and the path really exists. You are missing the most important thing, the logger. Just insert this code after your appender:

    (You can add more than one logger if you want to)


    <logger name="STANDART" additivity="false">
            <level value="ALL"/>
            <appender-ref ref="RollingFileAppenderSize"/>
        </logger>
    

    Your code should look like this:

    public enum LoggerTypes
    {
        STANDART = 0
    }
    public class Logger1
    {
        private static readonly string _loggerConfigDic = "..\\..\\YOURFOLDER\\YOURFILENAME.xml";
        private static readonly List<string> _names = new List<string> { "STANDART" }; /*Logger names in config file*/
    
        public static ILog GetLogger(LoggerTypes loggerType)
        {
            var fileInfo = new FileInfo(_loggerConfigDic);
            XmlConfigurator.ConfigureAndWatch(fileInfo);
            return LogManager.GetLogger(_names[(int)loggerType]);
        }
    }
    
    public class myClass
    {
        public void SomeMethod()
        {
            var logger = Logger1.GetLogger(LoggerTypes.STANDART);
            logger.Debug("your message here");
        }
    }