【问题标题】:MVC3 + ELMAH navigate to error detail?MVC3 + ELMAH 导航到错误详细信息?
【发布时间】:2012-07-26 14:08:48
【问题描述】:

我的应用:VS2010、MVC3、C#、最新的ELMAH

案例:发生一些错误(例如空引用),显示自定义错误页面(customErrors mode="On")。

任务:允许管理员(管理员角色的用户)从自定义错误页面查看错误详细信息

问题:如何获取/传递 elmah 错误 ID 到自定义错误页面视图?

更新:相关回答问题:ELMAH - Using custom error pages to collecting user feedback

自定义错误页面示例:

@model System.Web.Mvc.HandleErrorInfo
@{
  ViewBag.Title = "Error";
}
<h2>
  Error processing a request.
  @if (User.IsInRole(MyAppNamespace.Constants.ROLE_ADMIN))
  {
    // where to obtain ELMAH error id to navigate to error details ?
    // elmah/detail?id=291f5e83-5756-43bf-a889-07a548727da7
    <a href="@Url.Content("~/elmah")">View error details</a>
  }
</h2>

【问题讨论】:

  • 显示@Model.Exception.ToString()而不是链接到elmah错误页面会更容易。 Elmah 添加了很多关于您的环境的信息,但是如果您正在查看发生的错误,我不确定它是否有必要。

标签: asp.net-mvc logging razor elmah custom-error-pages


【解决方案1】:

这是 Elmah ErrorLog 模块中的一个事件,可以在 Global.asax.cs 中处理:

protected void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
    Session["ElmahId"] = args.Entry.Id;
}

然后我们可以使用存储的 id 来导航到错误(我使用的是 Elmah.Mvc 模块,它实现了特殊的控制器而不是默认的 elmah 页面)。在 Error.cshtml 中:

@if (User.IsInRole(renweb.Constants.ROLE_ADMIN))
{
      <a href="@Url.Action("Detail","Elmah",new{id=@Session["ElmahId"]})">Error details</a>
}

【讨论】:

    【解决方案2】:

    据我所知,所有 Elmah 错误都保存在一个名为 EMAH_Error 的表中。

    因此,您可以做的一件事是获取错误发生时的当前日期时间,并在表中查询该时间保存的记录给/花 1 分钟。查询将返回使用适当时间戳保存的错误,该时间戳也将包含 ErrorId。

    您可以轻松地使用 EF 或其他东西来创建和实体 ElmahError。

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多