Making log4net work with .NET 4.0 Client Profile

1 minute read

While creating one of the Open Source projects, I started using log4net library for logging.  But Apache haven't released log4net for Microsoft .NET 4.0 so for using log4net in a Class Library, you need to add reference to System.Web

Well, would you really like to do that?  No, of course not. The class library does not use any log4net AspNetTraceAppender they why reference System.Web?  At least, my class library does not require AspNetTraceAppender!   So we need a small patch to log4net to make it work with .NET 4.0 Client Profile

And if the consuming application decides to use the Class Library in a Web based project that requires AspNet tracing, they could use the replace the patched-log4net with the original-log4net

So here's the way to apply the patch

  • Download the log4net source code from Apache website
  • Open & upgrade the solution in Visual Studio 2010 and the conversion would go through fine!
  • Remove the System.Web project reference and add a reference to System.Configuration
  • Unload the Test project from the solution since that's not the
  • Exclude Appender\AspNetTraceAppender.cs class from the project
  • Comment the code in AssemblyInfo.cs
[sourcecode language='csharp']
[assembly: System.Security.AllowPartiallyTrustedCallers]
  • Framework change:
    • Navigate to Project -> log4net properties, and select the application tab
    • Change the target framework to .NET Framework 4.0 Client Profile
    • Change the Output Build Path to ..\build\bin\net\4.0\debug\
  • Configuration: DEBUG
    • Under Conditional compilation symbols, change this to NET;NET_1_0;NET_2_0;
    • When the symbol NET_2_0 is added, log4net uses System.Net.Mail (instead of System.Web.Mail) which is deprecated but solve the purpose for us.
  • Configuration: RELEASE
    • Under Conditional compilation symbols change this to STRONG;NET;NET_1_0;NET_2_0;
  • Optional:
    • Edit the Project Properties > Signing
    • Sign the assembly with a valid strong key
  • Compile the project in Release mode and distribute the new assembly
Note: The strong key does not match with the original release.