views:

31

answers:

1

We are using the approach described here to log our webservice errors with Elmah. And this actually works, but sadly the username beeing logged is empty.

We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User has the correct User set.

We also tried:

HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));

and

ErrorLog.GetDefault(null).Log(new Error(pError));

Without success.

Any ideas on how we can make Elmah log the username?

On a sidenote, when logging the error directly within the Webservice, the username is logged as expected. But taking this approach is not very DRY.

+1  A: 

Elmah take the user from Thread.CurrentPrincipal.Identity.Name and not from HttpContext.Current.User.

Since there ins't a convenient way to add custom data to Elmah, I would suggest recompiling the code, and calling HttpContext.Current.User instead.

Shay Erlichmen
Am I right that setting the Pricipal with: `Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("lala"),new string[0])' is a bad idea because of ThreadPooling?
Thomas Schreiner
Yes, you don't want to mess with thread principal
Shay Erlichmen
And if I Set it back to the old Principal after logging the error with elmah?
Thomas Schreiner
should work, use, try..finally, but its a hack...
Shay Erlichmen