Thursday, March 19, 2009

log4net with WCF Services

This week I ‘ve been working on adding tracing to a WCF IIS hosted application. It all started pretty easy since already has an appender for

So, I created a little wrapper around the log4net library and called the Debug function. My project had both aspx pages and svc services. These services were implemented in a separate dll (not in the web project). Oh! I forgot to mention that I wanted to use the AspNetRaceAppender so I could watch the trace in the trace.axd ‘page’.

But it was not working… I had a few debug entries and nothing appeared on the trace page. The ‘wired’ part was that if I’d copied and pasted the same debugging line in the aspx code behind’s it’d worked! So what’s up with that?

I started googling around I found close to nothing about log4net and WCF services. And that’s why I decided to post the solution here. Trying around many different scenarios I found the solution. You have to enable your wcf services to get the HttpCopntext from the web application. And in order to do that, you need to add the following line in your web app’s web.config file.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Add that line inside the system.serviceModel part. Also, you have to add the following attribute to your services’s classes.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Hope this helps somebody!

P.S: All the code I write is “works on my machine” certified ;)


1 comment:

Anonymous said...

Thanks, that did indeed help. ;)