使用Elmah为WCF Webservices记录用户名
我们正在使用此处所述的方法使用Elmah记录我们的Web服务错误。这实际上是有效的,但遗憾的是,记录的用户名为空。
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.
我们做了一些调试,发现在ErrorHandler中记录错误时, HttpContext.Current.User
有正确的用户设置。
We did some debugging and found, that when logging the error in the ErrorHandler the HttpContext.Current.User
has the correct User set.
我们还尝试过:
HttpContext context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(pError, context));
和
ErrorLog.GetDefault(null).Log(new Error(pError));
没有成功。
我们如何使Elmah登录用户名?
Any ideas on how we can make Elmah log the username?
在sidenote中,当直接在Webservice中记录错误时,用户名将按预期方式记录。但是采取这种方式并不是很干,
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.
Elmah将用户从 Thread.CurrentPrincipal.Identity .Name
而不是从 HttpContext.Current.User
。
Elmah take the user from Thread.CurrentPrincipal.Identity.Name
and not from HttpContext.Current.User
.
一个方便的方式来添加自定义数据到Elmah,我建议重新编译代码,并调用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.