Sunday, January 31, 2010

Using log4net with .Net 4.0 WPF Applications

I ran into some issues trying to get log4net up and running on my current project, which is being written in VS2010 Beta2, targeting the .net 4.0 framework.  I went about adding a reference to log4net, setup my app.config with a rolling log-file appender and added a call to XmlConfigurator.Configure() in one of my classes.

I then went to compile and got the following error: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?) 

I double-checked, and yes, I had indeed added a reference to log4net, so what was wrong?  Turns out, for .net 4.0 WPF projects, the default Target Framework is the “.Net Framework 4 Client Profile”, which is a subset of the full framework with the goal of shrinking footprint of the .net framework needed to download for a typical windows application.  However, log4net apparently needs access to system.web.dll, which isn’t included in the client profile. 

To fix your app so it’ll compile/run, open your project’s properties page, and set the Target Framework to “.Net Framework 4” and save your changes. 

29 comments:

  1. thank you so much.. i was about to die in despair :)

    ReplyDelete
  2. Ok, so now no Logging on the client???

    ReplyDelete
  3. thanks a lot

    ReplyDelete
  4. Great tip.

    ReplyDelete
  5. thanks I was pulling my hair out after I rebuilt my machine.

    ReplyDelete
  6. Thanks, just a crazy bug/feature.

    ReplyDelete
  7. Thanks, what a beating that was...

    ReplyDelete
  8. Thanks for saving me a few hours! :)

    ReplyDelete
  9. Helped a lot, thank you!

    ReplyDelete
  10. thanks a lot!

    salvou meu dia!!!

    ReplyDelete
  11. You saved my day! Thanx a lot!

    ReplyDelete
  12. Thank you so much for posting this!

    ReplyDelete
  13. Thanks - i thought i was going crrrrrazy :-)

    ReplyDelete
  14. More detailed answer here:

    http://tseonet.blogspot.com/2010/07/making-log4net-run-on-net-40.html

    ReplyDelete
  15. Thank you so much for this.

    ReplyDelete
  16. @Gregorio: Good call, there's an extra step for it to run that tseo seems to get. Also http://www.codeproject.com/KB/dotnet/Log4NetWithClient.aspx shows you how to build it to work for the Client Profile...

    ReplyDelete
  17. Thanks, this actually saved me. I have a 3rd party API that is a DLL and makes web service calls, so it needed the same fix. I couldn't understand why the reference was there but the messages implied they were not.

    ReplyDelete
  18. Thanks............!its working..........!

    ReplyDelete
  19. Thanks a lot!!! :)

    ReplyDelete
  20. thanks
    after a day of unnecessary work finally your post has solved my problem
    thanks thanks thanks

    ReplyDelete