Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all my log4net levels false?

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?

like image 312
John Smith Avatar asked Oct 19 '22 16:10

John Smith


People also ask

Is log4net structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.

Where are log4net logs stored?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation. The different log files are described in Services logs.


2 Answers

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();
like image 31
Ben Cameron Avatar answered Oct 22 '22 06:10

Ben Cameron


Change your line to following in AssemblyInfo.cs:

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

and put log4net's configuration in that file.

like image 19
Azimuth Avatar answered Oct 22 '22 04:10

Azimuth