kheya
kheya

Reputation: 7631

Why Log4Net is not writing the error logs?

I have tried several way to tweak but Log4Net file logger is not logging anything in my MVC3 web app.

What I am missing? I am not getting any error either.

The log4net dll is added to the application and it points to a local copy.

1 main web.config:

<appSettings>
    <add key="Log4NetConfigFilename" value="log4net.config" />
    <add key="Log4NetConfigFilePath" value="C:\DEV\App\source\App.Web\log4net.config" />    
  </appSettings>

2 Global.asax.cs

protected void Application_Start()
{
    log4net.Config.XmlConfigurator.Configure();
}

3 Log4net.config file:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <logger name="MyLogger">
    <level value="ALL" />
    <appender-ref ref="MyRollingLogFileAppender" />
  </logger>

  <appender name="MyRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="C:\\Support\Logs\\logs.log" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Size" />
    <param name="MaxSizeRollBackups" value="10" />
    <param name="MaximumFileSize" value="1024KB" />
    <param name="StaticLogFileName" value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821">      
      <param name="ConversionPattern" value="%date{MM-dd-yyyy HH:mm:ss} [%-5level] [%identity] - %message %newline" />      
    </layout>
  </appender>
</log4net>

#4 I have a folder created C:\Support\Logs
#5 The log4net.config file is located in C:\DEV\App\source\App.Web\log4net.config

6) controller where I am trying to log a string

These 2 values getting in the controller correctly

using log4net;
using log4net.Config;
using System.Configuration;
namespace My.Controllers
{    
    public class TestController : MyBaseController
    {
        private static readonly ILog _log = LogManager.GetLogger("MyLogger");
        private static readonly string _log4netConfigFilename = ConfigurationManager.AppSettings["Log4NetConfigFilename"];
        private static readonly string _log4netConfigFilePath = ConfigurationManager.AppSettings["Log4NetConfigFilePath"];

     [HttpGet]
     public ActionResult Index()
     {            
            if (_log.IsDebugEnabled) <-- ALWAYS FALSE                
                _log.DebugFormat("Processing Index() view");  <-- NEVER GETS WRITTEN

            var model = new MyModel();            
            return View(model);
      }
   }
}

Upvotes: 0

Views: 1005

Answers (2)

matth
matth

Reputation: 6309

You need to make sure that you are using the custom log4net.config by specifying the path when calling Configure:

log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["Log4NetConfigFilePath"]));

And as far as I know, you don't need the additional assembly info on the <layout> section of the config (was causing me errors):

<layout type="log4net.Layout.PatternLayout">      
  <param name="ConversionPattern" value="%date{MM-dd-yyyy HH:mm:ss} [%-5level] [%identity] - %message %newline" />      
</layout>

Upvotes: 0

BBlake
BBlake

Reputation: 2398

It's acting like it isn't seeing your configuration file. Try explicitly setting it to look at the configuration file by updating your App_Start code to the following:

log4net.Config.XmlConfigurator.Configure(ConfigurationManager.AppSettings["Log4NetConfigFilePath"]);

I'm not where I can test it at the moment but see if that gets it.

Upvotes: 2

Related Questions