How to enable Common.Logging.Log4Net

7k Views Asked by At

I am trying to enable Common.Logging.Log4Net to write all types of logs to a log file. The tutorials make it look so simple but I don't know what I am doing wrong. These are the steps I am taking:

  1. Create a new ASP.NET MVC empty project
  2. Install the "Common Logging Log4Net 1211" NuGet package
  3. Add the following lines to the default web.config:

    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
                <arg key="configType" value="INLINE" />
            </factoryAdapter>
        </logging>
    </common>
    
    <log4net>
        <root>
            <level value="ALL" />
            <appender-ref ref="FileAppender" />
        </root>
        <appender name="FileAppender" type="log4net.Appender.FileAppender" >
            <param name="File" value="C:\Users\MyName\Downloads\log.txt" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
    </log4net>
    
  4. Make sure the app. pool identity account has RW access to the path where I am trying to save the log file.

  5. Throw a random exception from code for testing purposes.

Am I missing anything? Is there a way I can debug log4net? Please help this poor soul. Thank you.

3

There are 3 best solutions below

1
On

The issue was related to the NuGet package for "Common.Logging.Log4Net" changing the name of the assembly. This actually fixed it (plesae note the new assembly name, being Common.Logging.Log4Net1211):

<common>
    <logging>
        <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
    </logging>
</common>
0
On

https://stackoverflow.com/a/756241/3469201

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

Then, to determine the file in which you want to save the output you can add the following code in the same .config file:

<configuration>
...

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

...
</configuration>
1
On

I'm not familiar with Common.Logging.Log4Net. Here is the simple log4net steps that I use -

  1. Install log4net from NuGet.
  2. Insert the following settings in web.config (you can configure the way you want)
  3. Then log.

If you want to use IoC container, you can read my question I asked last month.

web.config

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="FileAppender"/>
    </root>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="log-file.txt" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
  </log4net>  
</configuration>

Testing inside Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        XmlConfigurator.Configure();
        // You can inject it to your code using IoC container.
        var logger = LogManager.GetLogger(typeof(MvcApplication));
        logger.Debug("Test Debug.");
        logger.Error("Test Error.");
        logger.Fatal("Test Fatal.");
        logger.Info("Test Info.");
    }
}

Result

2015-06-04 11:48:05,885 [1] DEBUG DemoLog4Net.MvcApplication [(null)] - Test Debug.
2015-06-04 11:48:05,921 [1] ERROR DemoLog4Net.MvcApplication [(null)] - Test Error.
2015-06-04 11:48:05,922 [1] FATAL DemoLog4Net.MvcApplication [(null)] - Test Fatal.
2015-06-04 11:48:05,922 [1] INFO  DemoLog4Net.MvcApplication [(null)] - Test Info.