2

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

codeling
  • 11,056
  • 4
  • 42
  • 71
revolutionkpi
  • 2,632
  • 10
  • 45
  • 84
  • Please post the full content of your actual configuration file. The fragement(?) you show is surely invalid XML. Also note that the cause is actually shown in the screen ("Root element is missing"). – Christian.K Aug 27 '12 at 11:48
  • You don't really have the `protected static readonly ILog log = Log...` part in your configuration file, do you? – Christian.K Aug 27 '12 at 11:55
  • Your configuration file looks good and works in a simple ConsoleApplication. The error that is shown by the debugger-screenshot hints at an (syntactically) invalid XML/configuration file, which doesn't seem to be the case with the file you posted. – Christian.K Aug 27 '12 at 12:00
  • I have web config? and this is code from there – revolutionkpi Aug 27 '12 at 12:04
  • Web.config or app.config, never mind. If you _really_ posted your _complete_ configuration file, it is (syntactically) valid. – Christian.K Aug 27 '12 at 12:10
  • Try pasting your configuration into a separate .config file and use the `log4net.Config.XmlConfigurator.Configure(FileInfo fi)` procedure. – BrainPicker Aug 27 '12 at 12:11
  • now there is any error, but my log file is still empty – revolutionkpi Aug 27 '12 at 12:27
  • Change property "copy to output directory" of the config file to "copy if newer". – Renana Yacobi Aug 27 '12 at 12:40
  • have you tried running without the filters? are you still seeing the error? – Renana Yacobi Aug 27 '12 at 14:59
  • I don't have an error now. But it is not create file – revolutionkpi Aug 27 '12 at 15:13
  • are you using the web.config file or a custom file? if a custom, did you check it is copied to output? – Renana Yacobi Aug 27 '12 at 15:30
  • 1
    All this seems very strange to me as I was able to use your config file and your way of logging to successfully create a log file with the error and info messages in it. First I would recommend you to upgrade log4net to its latest version. Then I would recommend to use log4net's internal debug trace to find out the problem (you can post the trace in your question as well if you yourself are not able to find out the cause). http://logging.apache.org/log4net/release/faq.html (read How do I enable log4net internal debugging?) – Amit Mittal Aug 28 '12 at 05:32
  • On the same link also read, 'Why can't I log to a FileAppender from a web application?' – Amit Mittal Aug 28 '12 at 05:36

3 Answers3

5

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

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Prasad
  • 144
  • 2
  • 14
1

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));
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Irfan Ali
  • 67
  • 8
  • log file is still empty, I don't understand what I am doing wrong – revolutionkpi Aug 27 '12 at 14:04
  • IN 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 – Irfan Ali Aug 28 '12 at 10:10
0

Make sure you have [assembly: log4net.Config.XmlConfigurator] listed in AssemblyInfo.cs

I have found without adding that line everything appears to work with Log4Net but nothing gets logged.

-Edit-

This is my Log4Net config. I use a colored console appender, as well as a rolling file appender.

  <log4net>
        <appender name="Console" type="log4net.Appender.ColoredConsoleAppender">

            <mapping>
                <level value="INFO" />
                <foreColor value="Green,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="DEBUG" />
                <foreColor value="Cyan,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="WARN" />
                <foreColor value="Purple,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="ERROR" />
                <foreColor value="Red,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="FATAL" />
                <foreColor value="Yellow,HighIntensity"/>
            </mapping>

            <layout type="log4net.Layout.PatternLayout">
                <!-- Pattern to output the caller's file name and line number -->
                <conversionPattern value="%date,%5level,(%file:%line),%message%newline" />
            </layout>
        </appender>

        <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
            <file value="LOG.csv" />
            <appendToFile value="true" />
            <maximumFileSize value="1024MB" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />

            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date,%5level,(%file:%line),%message%newline"  />
            </layout>
        </appender>

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

Pay special attention to how my root element is listed - I think your config file issue may be there.

Try my config and see if it works for you. If it still does not work there are other issues that need to be addressed.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • This is not correct, I have never experienced this and it nowhere documented as such – Amit Mittal Aug 27 '12 at 12:38
  • @AmitMittal see post: http://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. – Robert H Aug 27 '12 at 13:01
  • I try this, but my log file is still empty – revolutionkpi Aug 27 '12 at 13:46
  • @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? – Robert H Aug 27 '12 at 14:23
  • @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. – Amit Mittal Aug 28 '12 at 05:26