【问题标题】:Retrieve HTTP error details in /error controller在 /error 控制器中检索 HTTP 错误详细信息
【发布时间】:2016-04-18 15:16:07
【问题描述】:

我正在使用customErrors mode="RemoteOnly" 并希望通过电子邮件通知向管理员报告错误详细信息。我不知道如何/在哪里在抛出 HTTP 错误时捕获详细的错误信息并调用 View - 特别需要 HTTP 500s。

我想在控制器中插入当前错误详细信息,标记为 Thread 的性能问题,并将“稍后”优化)。我正在使用 MVC 5.2.3.0。

我的web.config

<customErrors mode="RemoteOnly" defaultRedirect="/error/error">
    <error statusCode="400" redirect="/error/badrequest" />
    <error statusCode="403" redirect="/error/forbidden" />
    <error statusCode="404" redirect="/error/notfound" />
    <error statusCode="414" redirect="/error/urltoolong" />
    <error statusCode="500" redirect="/error/internalserver" />
    <error statusCode="503" redirect="/error/serviceunavailable" />
</customErrors>

我的errorInfo 视图模型:

namespace MyProject.ViewModels
{
    public class ErrorInfo
    {
        public string Code { get; set; }
    }
}

我的ErrorController(仅显示 HTTP 500):

public ActionResult InternalServer()
{
    ErrorInfo errorInfo = new ErrorInfo();
    errorInfo.Code = "500 - Internal Server Error";  <-- *ADD DETAILS HERE*
    SendHttpErrorAsync(errorInfo.Code);
    return PartialView("Error", errorInfo);
}

public void SendHttpErrorAsync(string message)
{
    Thread httpError = new Thread(delegate ()
        {
            message = User.Identity.IsAuthenticated ? message + " | " + User.Identity.GetUserName() : message + " | anonymous";
            message = message + " | " + Request.UserHostAddress; /*(::1 is localhost)*/
            try
            {
                var requestUri = string.Format("http://ip-api.com/xml/{0}", Request.UserHostAddress);
                var request = WebRequest.Create(requestUri);
                var response = request.GetResponse();
                var xdoc = XDocument.Load(response.GetResponseStream());
                message = (xdoc.Element("query").Element("status").Value == "success") ? message + " | " + xdoc.Element("query").Element("city").Value : message + " | location unvailable";
            }
            catch
            {
            }
            MyProject.Helper.Email.sendErrorEmail(message);
        });
        httpError.IsBackground = true;
        httpError.Start();
    }

【问题讨论】:

    标签: asp.net-mvc error-handling


    【解决方案1】:

    最好的选择是使用 Elmah for Asp.net - 所有功能都已内置。

    【讨论】:

      【解决方案2】:

      在 asp.net mvc 中,您可以在 Global.ascx 文件上的 Application_Error 事件中捕获应用程序级错误。这是link。我们可以定义 只需将以下方法添加到全局 ascx 文件中即可。

      protected void Application_Error(object sender, EventArgs e)
      {
          Exception ex = HttpContext.Current.Server.GetLastError();
          MyProject.Helper.Email.sendErrorEmail((ex.InnerException != null) 
                        ? ex.InnerException.Message
                        : "");
      
      }
      

      通常我更喜欢将异常记录到数据库中,除此之外,您还可以设置通知服务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多