0
votes

I use log4net in ASP.NET web service for logging. I added init logic for log4net to global.asax.

I tested this web servise on win xp, loging works.

Then I deployed this web service via msi installer on Windows Service 2003.

I test loging but it doesnt work. It doesn’t create log file.

First I show my code:

web.config

<?xml version="1.0"?>
<configuration>

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

  <appSettings>
  </appSettings>

  <connectionStrings/>

  <log4net configSource="config.log4net" />

  <system.web>    
    <authorization>
      <allow users="?" />
    </authorization>
    <compilation debug="true" ></compilation>
    <authentication mode="None" />
  </system.web>

</configuration>

log4net config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
    <appender name="RollinFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="logs\\log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="10" />
        <param name="MaximumFileSize" value="10" />
        <param name="RollingStyle" value="Date" />
        <param name="DatePattern" value="_yyMMdd.\t\x\t" /> 
        <param name="StaticLogFileName" value="false" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
        </layout>
    </appender>

    <logger name="mylogger">
        <level value="DEBUG" />
        <appender-ref ref="RollinFileAppender"/>
    </logger>
</log4net>

global.asax

public class Global : System.Web.HttpApplication
{    
    protected void Application_Start(object sender, EventArgs e)
    {
        XmlConfigurator.Configure();
    }
}

And I create logger object in WS class:

    public static ILog _logger = LogManager.GetLogger("mylogger");

usage in web method:

 _logger.Info(input);

First i think it is security problem.

So I edited permison on folder C:\Inetpub\wwwroot\txs\Logs for accounts:

  • ASPNET to Full Control
  • Network Service to Full Control
  • IUSR to Full Control

But it didn’t help.

So I edited settings on virtual folder on IIS to write. Also I didnt help.

I google it and found this: http://neilkilbride.blogspot.com/2008/03/log4net-problems-logging-from-web-app.html

So I tried it. Here is edited web.config.

<?xml version="1.0"?>
<configuration>

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

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

  <connectionStrings/>

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

  <log4net configSource="config.log4net" />

  <system.web>    
    <authorization>
      <allow users="?" />
    </authorization>
    <compilation debug="true" ></compilation>
    <authentication mode="None" />
  </system.web>

</configuration>

It didnt help. No log file is created. When I tested on win Xp everything works.

Can anybody give me advice what is root of problem.

EDITED: It was security issue, I put to web.config:

<trust level="High" />

and problem was solved.

1

1 Answers

2
votes

Try to give a full path for log file, you can also try to replace double slash "\" in your path to single one "\".

<log4net>
    <appender name="RollinFileAppender" type="log4net.Appender.RollingFileAppender">
        ...
        <param name="File" value="c:\logs\log" />
        ...
    </appender>
</log4net>

or try to see where the file is created

RollingFileAppender rootAppender = (RollingFileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Appenders[0];
string filename = rootAppender.File;

or you may re-check the write permission issues on server. It is more complicated on server then xp.