Log4net configuration error

20,591

Solution 1

A working configuration for Rolling File appender is like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
         <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
             <file value="logger.log" />
             <appendToFile value="true" />
             <maximumFileSize value="100KB" />
             <maxSizeRollBackups value="2" />
             <layout type="log4net.Layout.PatternLayout">
             <conversionPattern value="%date %logger [%thread] [%ndc] (%file:%line) %level- %message%newline"/>
             </layout>
         </appender>

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

and you should change your target framework to ".net framework 4" (by default it is different in my case) check this one will be useful

Solution 2

In AppConfig file use

<log4net debug="true">
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <layout type="log4net.Layout.XMLLayout" />
  <param name="File" value="BackgroundCheckLog/BackgroundCheckLog.log" />
  <param name="AppendToFile" value="true" />
  <param name="maximumFileSize" value="1000KB" />
  <param name="maxSizeRollBaenter code here`ckups" value="-1" />
  <layout type="log4net.Layout.PatternLayout">
    <header type="log4net.Util.PatternString" value="[START LOG] %newline" />
    <footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
    <conversionPattern value="%d [%t] %-5p (%file:%line) - %m%n" />
  </layout>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFile" />
</root>

protected static readonly ILog log = LogManager.GetLogger(typeof(TemplateController));

use

internal static ILog log = LogManager.GetLogger(typeof(TemplateController));
Share:
20,591
revolutionkpi
Author by

revolutionkpi

Updated on October 21, 2020

Comments

  • revolutionkpi
    revolutionkpi over 3 years

    I am using Log4Net to log exceptions in my web application.

    Here I have found an example for a configuration: http://www.csharptocsharp.com/log4net-configuration-for-rockin-loggin

    <?xml version="1.0"?>
    
    <configuration>
      <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
      </configSections>
    
      <log4net>
        <!--
      This writes the log information to the console window.  It only logs events
      that are at least at the INFO level (which would mean that DEBUG events are not
      captured.
      -->
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
          </layout>
          <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="INFO"/>
            <levelMax value="FATAL"/>
          </filter>
        </appender>
        <!--
      This stores information in the log.txt file.  It only captures log events
      that contain the key word test or error.
      -->
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
               <file value="L:\Name\trunk\Name.Web\log.txt"/>
          <appendToFile value="true"/>
          <rollingStyle value="Size"/>
          <maxSizeRollBackups value="5"/>
          <maximumFileSize value="10MB"/>
          <staticLogFileName value="true"/>
          <filter type="log4net.Filter.StringMatchFilter">
            <stringToMatch value="test"/>
          </filter>
          <filter type="log4net.Filter.StringMatchFilter">
            <stringToMatch value="error"/>
          </filter>
          <filter type="log4net.Filter.DenyAllFilter"/>
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
          </layout>
        </appender>
        <root>
          <level value="FATAL"/>
          <appender-ref ref="RollingFileAppender"/>
        </root>
        <logger name="Log4NetTest.OtherClass">
          <level value="DEBUG"/>
          <appender-ref ref="ConsoleAppender"/>
        </logger>
      </log4net>
    
    
    </configuration>
    

    In my class I have:

    protected static readonly ILog log = LogManager.GetLogger(typeof(TemplateController));
    

    and in my method I have:

    log4net.Config.XmlConfigurator.Configure();
    
                    //————————–
                    log.Error("sadi the great");
                    log.Info("sadi the great");
    

    but I have got error:

    enter image description here

  • Prasad
    Prasad over 11 years
    using Project-><solution_name>properties (using vs2010)
  • Amit Mittal
    Amit Mittal over 11 years
    This is not correct, I have never experienced this and it nowhere documented as such
  • Robert H
    Robert H over 11 years
    @AmitMittal see post: stackoverflow.com/questions/3898218/log4net-not-working where Kirk Woll states: "One gotcha for this type of thing is to make sure to add the XmlConfigurator attribute to the assembly by placing the following line in your AssemblyInfo.cs: [assembly: log4net.Config.XmlConfigurator] Otherwise log4net never activates." It has worked for me, along with that being the accepted answer with 33 upvotes. I think I can say it is correct.
  • revolutionkpi
    revolutionkpi over 11 years
    I try this, but my log file is still empty
  • revolutionkpi
    revolutionkpi over 11 years
    log file is still empty, I don't understand what I am doing wrong
  • Robert H
    Robert H over 11 years
    @revolutionkpi Can you update your question with your full app.config file, and if you followed other suggestions the config file for your log4net file as well?
  • Amit Mittal
    Amit Mittal over 11 years
    @RobertH Actually No :). The thing is to call log4net.Config.XmlConfigurator.Configure() to activate log4net which OP is already doing. The assembly attribute is just a shortcut (and in fact recommended by some) to call it automatically on your behalf.
  • Irfan Ali
    Irfan Ali over 11 years
    IN <param name="File" value="BackgroundCheckLog/BackgroundCheckLog.log" /> change value field where "BackgroundCheckLog" this is your current directory and "BackgroundCheckLog.log" is file name set these and try this work perfect for me