7

I have been attempting to get log4net logging in my asp.net web application with no success or any noticable errors. I am attempting to use the ADONetAppender appender with the following config:

<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="server=" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Context]) VALUES 
                            (@log_date, @thread, @log_level, @logger, @message, @exception, @context)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="32" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%t" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%p" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@context" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%x" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%c" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%m" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ADONetAppender" />
</root>

In my global.asax Application_Start I call

log4net.Config.XmlConfigurator.Configure();

Then to attempt to log:

protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{            
    log.Info("did I log yet?");
}

All of this does nothing as far as I can tell.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Luke Lowrey
  • 3,203
  • 3
  • 28
  • 40
  • 2
    I get the feeling that those connection string credentials are real, probably best to not include them in future posts... – si618 Jun 29 '09 at 10:02
  • I have the same problem - this is very frustrating. No log entries in the db, no errors from Log4Net's trace.. nothing. – znelson Apr 10 '12 at 21:05

3 Answers3

29

you can also set the following appsettings to enable internal log4net debugging information

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

The internal debug messages are written to the console (if you have one) or the trace (such as the debug window in visual studio or dbgview from sysintenals). This will help you to troubleshoot log4net issues.

softveda
  • 10,858
  • 6
  • 42
  • 50
  • 1
    Thanks, this helped me to find the Oracle exception being thrown by log4net and to resolve my issue immediately. – Joseph Yaduvanshi Jun 14 '10 at 17:33
  • 1
    If you're using DebugView, try checking the **Capture** > **Capture Global Win32** menu. I wasn't getting anything until I did that. – Eric Smith Feb 15 '12 at 00:13
  • 1
    Can you please tell me where exactly should I add this piece of code? I have a .Net 4.0 web application. – Germstorm Oct 23 '13 at 11:06
10

Internal debugging for log4Net is the way to go as suggested by @Pratik. To write the details into a file do the following:

Add the following in web.config even if you are using a different config file for log4net.

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

<system.diagnostics>
<trace autoflush="true">
  <listeners>
    <add
    name="textWriterTraceListener"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="C:\toolbox\SmartClient\log4net.txt" />
  </listeners>
</trace>
</system.diagnostics>

Reference

  1. How do I enable log4net internal debugging?
  2. log4net - Appenders not working in IIS7.5
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
8

Have you added added the following section under under Configuration->Configsections

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

Although it is possible to add your log4net configuration settings to your project’s app.config or web.config file, it is preferable to place them in a separate configuration file. Aside from the obvious benefit of maintainability, it has the added benefit that log4net can place a FileSystemWatcher object on your config file to monitor when it changes and update its settings dynamically.

To use a separate config file, add a file named Log4Net.config to your project and add the following attribute to your AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

Note: for web applications, this assumes Log4Net.config resides in the web root. Ensure the log4net.config file is marked as “Copy To Output” -> “Copy Always” in Properties.

From here.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    so, if you do "Copy To Output" won't that put the log4net.config in your web app's bin directory as well as the site root? – dotjoe Nov 15 '12 at 21:39