Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms

Making Log4Net Work with the .NET Client Profile

Rate me:
Please Sign up or sign in to vote.
4.94/5 (18 votes)
12 Sep 2010CPOL2 min read 71.3K   2K   34   11
A step by step guide to getting Log4Net working with the .NET Client Profile

Background

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?)

There are some open tickets (LOG4NET-174, LOG4NET-233) raised against this issue, but there is no indication of when (or if) this will be resolved.

The Solution

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.

  1. Download the log4net source
  2. Open & upgrade the solution using Visual Studio 2010
  3. Remove the System.Web project reference
  4. Exclude Appender\AspNetTraceAppender.cs class from the project
  5. Add a reference to System.Configuration
  6. Navigate to Project -> log4net properties, and select the application tab
  7. Change the target framework to .NET Framework 3.5 Client Profile
  8. Select the Build tab, and change the configuration to Debug
  9. Under Conditional compilation symbols, change this to NET;NET_1_0;NET_2_0;
  10. Change the configuration to Release
  11. Under Conditional compilation symbols change this to STRONG;NET;NET_1_0;NET_2_0;
  12. Edit the AssemblyInfo.cs class and update the AssemblyKeyFile attribute with a valid strong key
  13. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 20 years experience in a wide range of technologies.

@TheCodeKing

Comments and Discussions

 
GeneralMy vote of 5 Pin
fredatcodeproject9-Aug-13 5:04
professionalfredatcodeproject9-Aug-13 5:04 
GeneralMy vote of 1 Pin
Clifford Nelson11-Jun-12 6:16
Clifford Nelson11-Jun-12 6:16 
QuestionMissing Reference Pin
rob.evans16-May-12 9:42
rob.evans16-May-12 9:42 
GeneralMy vote of 5 Pin
Martin Capodici19-Dec-11 9:02
Martin Capodici19-Dec-11 9:02 
Newslog4net is updated Pin
Alexey Merson7-Nov-11 22:07
Alexey Merson7-Nov-11 22:07 
GeneralRe: log4net is updated Pin
Martin Capodici19-Dec-11 9:00
Martin Capodici19-Dec-11 9:00 
GeneralMy vote of 5 Pin
agarob30-Sep-10 4:49
agarob30-Sep-10 4:49 
GeneralFramework 4 Client Profile Pin
jef_ti11-Sep-10 11:39
jef_ti11-Sep-10 11:39 
GeneralRe: Framework 4 Client Profile Pin
TheCodeKing12-Sep-10 0:02
TheCodeKing12-Sep-10 0:02 
GeneralNice detective work Pin
DThrasher6-Sep-10 14:57
DThrasher6-Sep-10 14:57 
GeneralRe: Nice detective work Pin
TheCodeKing6-Sep-10 19:52
TheCodeKing6-Sep-10 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.