Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net logging not working - asp.net mvc

My log4net configuration is this,

    <?xml version="1.0" encoding="utf-8" ?>
     <log4netConfiguration>
     <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
  </appSettings>
  <log4net>   
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="C:\my_logs/my_web_logs/my_log_%date{ddMMyyyy}.log" />
      <appendToFile value="true" />      
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="30MB" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
        <!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />-->
      </layout>
    </appender>   
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</log4netConfiguration>

I have a Logger helper class as,

public static class Logger
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static log4net.ILog Log
        {            
            get { return log; }
        }
    }

In my assembly info, I have this entry,

// Configure log4net using the .config file 
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

log4net.config is the config file added to the web project.

In the code I log using the logger class,

Logger.Log.Info("User visits Sign In Page.");

Logging has been working when I set up the above setting. But suddenly logging has stopped working. But when I created a new asp.net mvc website with above settings, logging works for that. I tried with IIS Express and Local IIS. In both cases logging works for the test application I have created.

I cannot figure out why it's not logging? How can I diagnose this? What are the possible issues?

like image 330
Dhanuka777 Avatar asked Jan 14 '14 07:01

Dhanuka777


Video Answer


1 Answers

Solved by myself, reason was "for some reason" log4net configuration was not loaded from assembly info. Still I do not know why that happens.

I tried so many fixes proposed by different posts. Finally fixed the issue. Solution mentioned in this post helped me to solve the issue.

I have added following configuration,

    <!--These settings load the log4net configuration.-->
        <add key="log4net.Config" value="log4net.config"/>
        <add key="log4net.Config.Watch" value="True"/>

It starts logging!

Then I removed following line from assembly info,

// Configure log4net using the .config file 
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
like image 94
Dhanuka777 Avatar answered Sep 28 '22 02:09

Dhanuka777