使用 Elmah 记录 WCF Web 服务的用户名

发布于 2024-09-26 18:01:28 字数 610 浏览 2 评论 0原文

我们使用此处描述的方法来记录我们的Elmah 的 Web 服务错误。这确实有效,但遗憾的是记录的用户名是空的。

我们进行了一些调试,发现在 ErrorHandler 中记录错误时,HttpContext.Current.User 具有正确的用户集。

我们也尝试过:

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

并且

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

没有成功。

关于如何让 Elmah 记录用户名有什么想法吗?

顺便说一句,当直接在 Web 服务中记录错误时,会按预期记录用户名。但采用这种方法并不是很 DRY。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-10-03 18:01:28

Elmah 从 Thread.CurrentPrincipal.Identity.Name 获取用户,而不是从 HttpContext.Current.User 获取用户。

由于没有方便的方法将自定义数据添加到 Elmah,我建议重新编译代码,并改为调用 HttpContext.Current.User。

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.

零度℉ 2024-10-03 18:01:28

这是我一遍又一遍地看到的问题。虽然可以重新编译代码,但我宁愿建议使用 ELMAH 中内置的功能,如我的博客文章 使用错误过滤钩子丰富 ELMAH 错误

在您的情况下,可以通过添加 ErrorLog_Filtering 方法来设置所有错误的 User 属性:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
    var httpContext = args.Context as HttpContext;
    if (httpContext != null)
    {
        var error = new Error(args.Exception, httpContext);
        error.User = httpContext.User.Identity.Name;
        ErrorLog.GetDefault(httpContext).Log(error);
        args.Dismiss();
    }
}

This is a question that I see over and over again. While re-compiling the code is a possibility, I would rather suggest using features built into ELMAH already, as explained in my blog post Enrich ELMAH errors using error filtering hook.

In your case, setting the User property on all errors, can be achieved by adding the ErrorLog_Filtering-method:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
    var httpContext = args.Context as HttpContext;
    if (httpContext != null)
    {
        var error = new Error(args.Exception, httpContext);
        error.User = httpContext.User.Identity.Name;
        ErrorLog.GetDefault(httpContext).Log(error);
        args.Dismiss();
    }
}
惟欲睡 2024-10-03 18:01:28

作为重新编译 Elmah 的替代方法,我们可以使用全局方法,该方法在调用 elmah 之前填充 Thread.CurrentPrincipal.Identity.Name。

public static void LogError(Exception exception, string username)
{
    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string[] {});
    Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}

As an alternative to recompile Elmah, we can use a global method, which populates Thread.CurrentPrincipal.Identity.Name before calling elmah.

public static void LogError(Exception exception, string username)
{
    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(username), new string[] {});
    Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文