Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I doing wrong with this log4net implementation?

Tags:

c#

log4net

Like many others, I'm trying to implement Log4net into my C#.net application and I just can't get it to work for me. I've done a lot of searching (including on this site) and have tried many different solutions but nothing has worked for me yet. So the simple question: What am I doing wrong???

Assembly.cs contains the following line.

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

App.config contains the following.

<configSections> 
  <section name="log4net" 
           type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 
</configSections>
<log4net> 
  <root> 
    <level value="ALL"/> 
    <appender-ref value="RollingFileAppender"/> 
    <appender-ref value="ConsoleAppender"/> 
  </root>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">  
    <param name="File" value="C:\Temp\Minion.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">  
      <conversionPattern value="%date{Y-m-d H:i:s.u} | %thread | %method | %level | %message%newline" />
    </layout>
  </appender>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <param name="Header" value="[Header]\r\n" />
      <param name="Footer" value="[Footer]\r\n" />
      <param name="ConversionPattern" value="%date{Y-m-d H:i:s.u} | %thread | %method | %level | %message%newline" />
    </layout>
  </appender>
</log4net>

My code contains the following at the start of the class.

private static readonly ILog logger =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And these tests at the start of the main function.

logger.Debug("test1");
logger.Info("test2");
logger.Warn("test3");
logger.Error("test4");
logger.Fatal("test5");

EDIT:

A tip led to the following errors during log4net initializaiton.

log4net:ERROR XmlHierarchyConfigurator: No appender named [] could be found.
log4net:ERROR Appender named [] not found.
log4net:ERROR XmlHierarchyConfigurator: No appender named [] could be found.
log4net:ERROR Appender named [] not found.
like image 282
Lodra Avatar asked Jan 10 '23 14:01

Lodra


1 Answers

Change this:

<appender-ref value="RollingFileAppender"/> 
<appender-ref value="ConsoleAppender"/> 

to this:

<appender-ref ref="RollingFileAppender"/> 
<appender-ref ref="ConsoleAppender"/> 

And see if it won't cooperate better.

like image 137
Jeroen Mostert Avatar answered Jan 12 '23 02:01

Jeroen Mostert