Why are all my log4net levels false?

John Smith picture John Smith · Dec 28, 2011 · Viewed 31.5k times · Source

I'm using log4net in my ASP.NET MVC3 project, but all logging properties such as IsDebugEnabled == false

In my AssemblyInfo I have:

[assembly: XmlConfigurator(Watch = true)]

In my log class I have

public Log4NetLogger()
{
    log4net.Config.XmlConfigurator.Configure();
    Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}

My related config stuff in Web.Config is:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
       </sectionGroup>
  </configSections>

    <log4net debug="false">
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="100" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="{removed}" />
        <commandText value="INSERT INTO Logging ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
        <parameter>
          <parameterName value="@log_date" />
          <dbType value="DateTime" />
          <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
          <parameterName value="@thread" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@log_level" />
          <dbType value="String" />
          <size value="50" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@logger" />
          <dbType value="String" />
          <size value="255" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@message" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
          </layout>
        </parameter>
        <parameter>
          <parameterName value="@exception" />
          <dbType value="String" />
          <size value="2000" />
          <layout type="log4net.Layout.ExceptionLayout" />
        </parameter>
      </appender>
      <!--Possible levels:-->
      <!--DEBUG-->
      <!--INFO-->
      <!--WARN-->
      <!--ERROR-->
      <!--FATAL-->
      <root>
        <level value="All" />
        <appender-ref ref="AdoNetAppender" />
      </root>
    </log4net>

  </applicationSettings>

</configuration>

I already got frustrated to a point of just wanting to do

public Log4NetLogger()
{
    Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


    Logger.IsDebugEnabled = true;
}

However of course Logger.IsDebugEnabled does not have any setters :/

What do I have to do to get this damn thing to work?

Answer

Ben Cameron picture Ben Cameron · Apr 2, 2013

I have had this same problem. I get around it by using the below line of code in the Application_Start method in the Global.asax.cs (assuming its a .Net web application)

log4net.Config.XmlConfigurator.Configure();