Web Development Blog

Problem getting log4net 1.2.10 working with ASP.Net 4

Just a quick note on getting log4net 1.2.10 working with ASP.Net 4.

I had been working with log4net version 1.2.10.0 in ASP.Net 3.5 and it had been working but after upgrading the project to ASP.NET 4 I noticed an odd behavior. It would log only one message to the log file and refused to log any further messages until the application was restarted.

log4net Configuration File

Here is a the log4net configuration I was using which is in a separate file in the root directory

<?xml version="1.0" encoding="UTF-8"?> 
<log4net> 
    <logger name="Application"
        <level value="ERROR" /> 
        <appender-ref ref="GeneralAppender" /> 
    </logger> 
    <logger name="NHibernate"
        <level value="ERROR" /> 
        <appender-ref ref="NHibernateAppender" /> 
    </logger> 
    <appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender"
        <threshold value="ERROR" /> 
        <file value="App_Data/Logs/error.log" /> 
        <appendToFile value="true" /> 
        <rollingStyle value="Date" /> 
        <datePattern value="'.'yyyyMMdd'.log'" /> 
        <layout type="log4net.Layout.PatternLayout"
            <conversionPattern value="%d %-4r [%t] %-5p %c %x - %m%n"/> 
        </layout> 
    </appender> 
    <appender name="NHibernateAppender" type="log4net.Appender.RollingFileAppender"
        <threshold value="ERROR" /> 
        <file value="App_Data/Logs/nhibernate.log" /> 
        <appendToFile value="true" /> 
        <rollingStyle value="Date" /> 
        <datePattern value="'.'yyyyMMdd'.log'" /> 
        <layout type="log4net.Layout.PatternLayout"
            <conversionPattern value="%d %-4r [%t] %-5p %c %x - %m%n"/> 
        </layout> 
    </appender> 
</log4net> 
 

Loading the Configuration

As recommended the configuration is loaded on the application start event.

private void Application_Start() 
      var configFile = new FileInfo(Server.MapPath("~/log4net.config")); 
      XmlConfigurator.Configure(configFile); 
 

After excessive searching Google and trying to find a solution I came up empty.

In the end the solution was quite straight forward I just downloaded the 1.2.11.0 release and it just worked. So I'm guessing 1.2.10.0 just isn't compatible with ASP.Net 4, or at least my project.




Running Lucene.Net under Medium Trust

I'm in the middle of moving a web application from a Full Trust environment to a Medium Trust environment. There was all sorts of things that wouldn't work under Medium Trust as they did under Full Trust. I updated the version of Lucene.Net I was using to Lucene.Net 2.9.2 and of course it wouldn't work out of the box.

This post describes the problem almost exactly.

When I was updating the search index I got the following error.

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
FileSupport.Sync(FileStream fileStream) +0
Lucene.Net.Store.FSDirectory.Sync(String name) +151
Lucene.Net.Index.SegmentInfos.FinishCommit(Directory dir) +171
Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean create, Boolean closeDir, IndexDeletionPolicy deletionPolicy, Boolean autoCommit, Int32 maxFieldLength, IndexingChain indexingChain, IndexCommit commit) +257
Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean closeDir, IndexDeletionPolicy deletionPolicy, Boolean autoCommit, Int32 maxFieldLength, IndexingChain indexingChain, IndexCommit commit) +110
Lucene.Net.Index.IndexWriter..ctor(Directory d, Analyzer a, MaxFieldLength mfl) +369

So I looked at each of the methods in the SupportClass.FileSupport.Sync(FileStream fileStream) method that would throw a SecurityException simply commenting out the code after fileStream.Flush() resolves the security problem.

/// <summary> 
/// Flushes the specified file stream. Ensures that all buffered 
/// data is actually written to the file system. 
/// </summary> 
/// <param name="fileStream">The file stream.</param> 
public static void Sync(System.IO.FileStream fileStream) 
    if (fileStream == null
    { 
        throw new ArgumentNullException("fileStream"); 
    } 
    fileStream.Flush(); 
 
    //if (OS.IsWindows) 
    //{ 
    //    if (!FlushFileBuffers(fileStream.Handle)) 
    //        throw new System.IO.IOException(); 
    //} 
    //else if (OS.IsUnix) 
    //{ 
    //    if (fsync(fileStream.Handle) != IntPtr.Zero) 
    //        throw new System.IO.IOException(); 
    //} 
    //else 
    //{ 
    //    throw new NotImplementedException(); 
    //} 

I recompiled and so far so good I can update the search index under Medium Trust.

 

Using WSE 3.0 in VS 2010 with .NET 4.0

I had a project that used Web Services Enhancements or commonly WSE 3.0 which I recently decided to migrate to .NET 4.0, it had previously been using .NET 3.5. Ina previous post described how to use WSE 3 in Visual Studio 2010