gilad
gilad

Reputation: 297

unable to write logs to a file using log4net in an ASP.NET Web Forms Site

I'm having a problem logging to a text file using log4net dll.

I'm using vs 2012 express on a windows server 2008 r2 standard (64). I have preform the following steps:

  1. Open a new asp.net web forms
  2. Install log4net via nuget manager.
  3. in the global.asax file I have this in Application_Start :

    log4net.Config.XmlConfigurator.Configure ();
    
  4. In the web config I have added to the configSections node this line :

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

  5. In the web config I have added this section :

    <log4net debug="true">
     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
     <file value="c:\webSite\extraDownloadServerResources\Logs\ALU\log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Composite"/>
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="5MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%n%-5p %d %5rms %-22.22c{1} %-18.18M %n - %m%n" />
    </layout>
    </appender>
    <root>
     <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    </log4net>
    
  6. In a start up page I have fetch instance of the logger :

    private static readonly ILog log = LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType );
    
  7. In the Page_Load I have wrote this line :

    log.Debug ( "test" );

  8. I have add to the log.txt file security the Dedualt App Pool user with read/write permissions.

I have ended up with nothing in my log file. To test the steps I did I have created a new empty web site and follow these steps one by one. alas (I like this word) this time the log have been written to the file.

Can any one point me to a possible solution ?

Upvotes: 1

Views: 3110

Answers (2)

gilad
gilad

Reputation: 297

i have managed to solve my problem.

i have downloaded a tool to monitor all iss trafic. When monitoring the traffic i saw the user name wasn't defaultAppPool nor AppPoolName. it was ASP.NET v4.0. After i granted the permissions and restarted everything , i finally saw some logs in the file...

Upvotes: 0

Alexander Manekovskiy
Alexander Manekovskiy

Reputation: 3203

To debug issues with log4net configuration you should enable log4net internal debugging.

Inside appSettings add log4net.Internal.Debug key as following:

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

This will enable log4net to output all messages to console and to the Trace. To forward all diagnostics messages to file you can add a trace listener:

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

These snippets were taken directly from the log4net FAQ and here only for quick reference.

This at least will give you understanding of what is going on as you will get error messages.

Upvotes: 2

Related Questions