Why are all my log4net levels false?

2019-02-07 17:51发布

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?

13条回答
混吃等死
2楼-- · 2019-02-07 18:05

It seems that you missed config file property setting changes Copy to Output Directory = "Copy always" Please check this setting image for more details. Config file setting Properties

查看更多
在下西门庆
3楼-- · 2019-02-07 18:05

Change your line to following in AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Ensure log4net dll is added and Also put log4net's configuration in web.config file as

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" />
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\LOGS\IDMUserRoleManagement\IDMUserRoleManagement.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="1000KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{yyyy-MM-ddTHH:mm:ss} %-5p [%t] - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-07 18:08

if class log file (Log4NetLogger) is in a alone class library, then the code: [assembly: log4net.Config.XmlConfigurator(C.... should be in the same class library.

for example: lib 1: my.web (a mvc project) lib 2: my.common (a class library project)

if you wrap logmanager code in my.common, then "[assembly: log4net.Config.XmlConfigurator(C..." SHOULD BE IN my.common, if you put it in my.web, it will not work!

Edit:

The usage of all config files (app.config, web.config, log4net.config, etc) is context based. So if you have a app calling a class library the config file that will be used is the .Config in the App project not the class library.

Another example, if you're Unit Testing an app the context is the Unit Test project and that's which config file will be used, not the app's config.

查看更多
我命由我不由天
5楼-- · 2019-02-07 18:09

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();
查看更多
Deceive 欺骗
6楼-- · 2019-02-07 18:14

Log levels are case-sensitive, so instead of:

<level value="All" />

should be

<level value="ALL" />

I also find it much easier to create a separate log4net configuration file. Create a file called log4net.config and set the property of Copy to Output Directory to Copy always -- (copying your configuration from App.config into this file)

Then, when setting the configuration use:

XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));

查看更多
家丑人穷心不美
7楼-- · 2019-02-07 18:14

You should have failed to initialize log4net in your application. This can be initialized explicitly by placing below at 'Application_Start',

log4net.Config.XmlConfigurator.Configure();

查看更多
登录 后发表回答