Log4net for Client Profile
I've recently been working on a desktop application targeted at the Microsoft .Net Framework 4 Client Profile and ran into a problem using log4net. The issue is that the .Net Client Profile does not include any web related assemblies which are required by the log4net assembly. In particular for the AspNetTraceAppender class.
The result is that the C# compiler cannot resolve the log4net references and this results in a "reference not found" compiler error. When using Visual Studio it appears as if it cannot find the log4net dll when the project is built, but it's actually caused by missing dependencies. The error displayed is as follows:
Error 1 The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)
Luckily log4net is open source and the solution is relatively straight forward, although perhaps not entirely obvious. The following steps will allow you to build a custom version of log4net that will work with an application targeted at the 3.5 or 4 Client Profile.
- Download the log4net source
- Open & upgrade the solution using Visual Studio 2010
- Remove the System.Web project reference
- Exclude Appender\AspNetTraceAppender.cs class from the project
- Add a reference to System.Configuration
- Navigate to Project -> log4net properties, and select the application tab
- Change the target framework to .NET Framework 3.5 Client Profile
- Select the Build tab, and change the configuration to Debug
- Under Conditional compilation symbols change this to NET;NET_1_0;NET_2_0;
- Change the configuration to Release
- Under Conditional compilation symbols change this to STRONG;NET;NET_1_0;NET_2_0;
- Edit the
AssemblyInfo.csclass and update the AssemblyKeyFile attribute with a valid strong key
- Compile the project in Release mode and distribute the new assembly
Note log4net must be compiled for 3.5 Client Profile and not 4 Client Profile. This allows the assembly to work correctly with both versions of the framework. For some reason compiling for 4 results in a runtime error deep with the framework which I didn't have time to resolve.
For convenience I've provided a version of the log4net dll compiled for client profile via this page, but be aware that the strong name does not match that of the official release.