Search code examples
wcfloggingiis-7iis-7.5log4net

Why does log4net stop logging after a while on .NET WCF but not a usual website?


I've search many sites and tried several different opinions. But I still could not solve it. Here are the things I did right now:

  • In Global.asax at Application_Startup I give the file path and startup the Log4Net.
  • Right after I start log4net, I write a log that says "Application has stared'"
  • Currently, there is only 1 worker in IIS for the WCF application The IIS user has access to WRITE, MODIFY and READ privileges

The problem:

  1. When I invoke a method of service directly (without doing the 2. step below), No Logs is written
  2. On a browser, I write te WCF url and hit ENTER, Log4Net creates the folder and the files (files are EMPTY at this point).
  3. If I make requests and invoke the methods (doing the 1st step), now Log4Net writes the logs.

The actual problem:

  • After the 3rd step, (lets say we waited without any invokes of the WCF methods around 10 minutes or more), the invoking DOES NOT CREATE Log4Net Text logs ANYMORE.
  • Sometimes, if I repeat the 2nd step, it begins writing the logs again. But there is no coherent results.

Here is the Config.xml:

 <?xml version="1.0" encoding="utf-8"?>

    <log4net>
       <appender name="ProcessInfo_FileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="L:\LOGs\ProcessInfo\ProcessInfo_[%processid].txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="200" />
    <maximumFileSize value="30MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] - %message%newline" />
    </layout>
  </appender>

     <logger name="ProcessInfo">
        <levelMin value="ERROR" />
        <levelMax value="INFO" />
        <appender-ref ref="ProcessInfo_FileAppender" />
      </logger>  
    <root></root>
    </log4net>

I have other WCF projects which have no problem even with multiple Workers. (I used the exact same IIS and Log4Net xml configuration with them). Also, as I mentioned on the title, I have a WebSite who has exact same logging codes (they both using a common 3rd party dll which I wrote) and has NO PROBLEM of writing Log4Net text logging at all.

Please help.

Thanks.


Solution

  • The problem is not in your logging configuration, you should try to enable log4net internal debugging. This will tell you why the logging stops. I guess there is some code that reconfigures your logging to load configuration from your web.config which is not there.

    <configuration>
    ...
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    
    ...
    
        <system.diagnostics>
            <trace autoflush="true">
                <listeners>
                    <add 
                        name="textWriterTraceListener" 
                        type="System.Diagnostics.TextWriterTraceListener" 
                        initializeData="C:\tmp\log4net.txt" />
                </listeners>
            </trace>
        </system.diagnostics>
    </configuration>
    

    Log4net FAQ