Log4net configuration from assembly attribute does not load configuration file

finally i just find the simple solution, you may get help there

  1. Global.asax in start function

     protected void Application_Start()
     {
       log4net.Config.XmlConfigurator.Configure();
     }
    
  2. In any of the class where use logging at class level

add namespace

using log4net;

add this code line at class level

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  1. use log function in any of the action call

    log.Error("test error q111..");
    
  2. configuration

    <configuration>
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
    </configSection>
    <log4net debug="true">
    
    <!--AdoNet appender is use for write log file into sql server-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=DESKTOP-NLH31FH; Initial Catalog=SmallBizDb;Integrated Security=true" providerName="System.Data.SqlClient" />
      <commandText value="INSERT INTO [dbo].[Logs] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@logdate,@thread, @loglevel, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@logdate" />
        <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="@loglevel" />
        <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>
    <!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
    
    <root>
      <level value="Debug" />
      <!--<appender-ref ref="RollingLogFileAppender" />-->
      <!--Enable this line if you want write log file into plain text file-->
      <appender-ref ref="AdoNetAppender" />
      <!--Enable this line if you want write log file into sql server-->
    
    </root>
    
    </log4net>
    
    <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
    </configuration>
    

it may help all and easy to use. thanks


I do keep log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config"))); in the Global.asax.cs inside of the Application_Start()... So I don't need to carry the command all over the places.


I also have trouble with this method of boostrapping log4net. The documentation says you have to make a call very early in your application startup

if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Try placing a logger in the same class that starts your application (program.cs, app.xaml, whatever). For example

private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

And for kicks, make any call to the log (even one that is filtered or evaluated out of the append process, it should force log4net to evaluate your repository/heirarchy).

static Program()
{
    Log.Debug("Application loaded.");
}