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?

有帮助吗?

解决方案

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)]

其他提示

The reason is that Log4Net tries to load the config from the assembly that first uses LogManager.GetLogger(). If it's one of your class libraries it will simply ignore the attribute in all other assemblies.

The easiest way to fix it is to invoke LogManager in your start file (like Program.cs or Global.asax):

var logger = LogManager.GetLogger(typeof(Program));
logger.Info("Application started.");

//rest of app init.

Doing that will get you the expected behavior with the assembly attribute.

I found log4net won't load the webconfig configuration unless you call log4net.Config.XmlConfigurator.Configure(); on start up.

    protected void Application_Start()
    {
        // your other codes

        log4net.Config.XmlConfigurator.Configure(); // must have this line
        Logger = log4net.LogManager.GetLogger(typeof(MvcApplication));

    }

I had a similar issue: I would not get any logging output, when I ran my assembly from IIS or IISExpress.

However, none of the answers above worked for me.

In my case the solution was to specify the path to the config file as an absolute path. It turned out that IISExpress does not set the current directory to the bin folder and log4net would not find the config file, so I had to use this workaround:

        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        // remove file:// part from uri
        UriBuilder ub = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(ub.Path);

        var fi = new FileInfo(Path.Combine(Path.GetDirectoryName(path), "Logging.config.xml"));

        if (fi.Exists)
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);

            var logger = log4net.LogManager.GetLogger(typeof(WebApiApplication));
            logger.Info("Application started.");
        }
        else
        {
            throw new FileNotFoundException("log4net config file not found", fi.FullName);
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top