简体   繁体   中英

Log4net not working in IIS7 on windows server 2008

Logs are not created on the server (Windows 2008) and IIS7. I had given absolute path as well as relative path.

I had given all rights to the log folder. I had included network user, IUsr and IIS_IUSRS, and gave permission to every one. also.

It is not writing logs to that folder

the entries on the web config is as follows

 <section name="log4net"   type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

 <log4net>
 <logger name="GeneralLogger">
 <level value="ALL" />
 <appender-ref ref="RollingFile" />
 </logger>
 <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
 <file value="C:\\vhosts\\staging.customerportal.com\\log\\CustomerPortal.log"/>
 <appendToFile value="true"/>
 <datePattern value="yyyyMMdd"/>
 <rollingStyle value="Date"/>
 <filter type="log4net.Filter.LevelRangeFilter">
 <acceptOnMatch value="true"/>
 <levelMin value="DEBUG"/>
 <levelMax value="FATAL"/>
 </filter>
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%-5p %d %5rms %X{addr} %-22.22c{1} %-18.18M - %m%n"/>
 </layout>
 </appender>
 </log4net>

I had tried giving single slash and well as double slash in the file value in web config nothing works.

The code in the global.asax.cs is

 public class MvcApplication : System.Web.HttpApplication
 {
   private static log4net.ILog log =   log4net.LogManager.GetLogger(typeof(MvcApplication));

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

    }

code in assemblyinfo.cs is

   [assembly: XmlConfigurator(ConfigFile="web.config", Watch=true)]

I had tried with this code and also without this code in assemblyinfo.cs

It is not working.

Where as When I use absolute path in localhost the logs are written to that folder properly

It fails on the server

I had tried both these options in the controller file

  //private static log4net.ILog log =     
        log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)      ;
   private static log4net.ILog log = log4net.LogManager.GetLogger("GeneralLogger");

I had tried a path which does not exists, it did not created folder.

Please help me. Please help me how to enable logs using log4net in server

You are initializing your logger before configuring the logmanager (actually you configuring it twice) by calling the Configure on Application_Start.

Option 1:

public class MvcApplication : System.Web.HttpApplication
{
private static log4net.ILog log =   log4net.LogManager.GetLogger(typeof(MvcApplication));

protected void Application_Start()
{ 
  //Remove: log4net.Config.XmlConfigurator.Configure();

}

And keep:

  [assembly: XmlConfigurator(ConfigFile="web.config", Watch=true)]

Option 2:

public class MvcApplication : System.Web.HttpApplication
{
private static log4net.ILog log;

protected void Application_Start()
{ 
  log4net.Config.XmlConfigurator.Configure();
  log = log4net.LogManager.GetLogger(typeof(MvcApplication));
}

And remove:

//Remove:  [assembly: XmlConfigurator(ConfigFile="web.config", Watch=true)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM