【问题标题】:error handling asp.net mvc 5 doesn't let stack trace to be viewed on event viewer错误处理 asp.net mvc 5 不允许在事件查看器上查看堆栈跟踪
【发布时间】:2014-08-16 11:40:05
【问题描述】:

我在 web.config 中处理了错误 500 和 404

 <httpErrors errorMode="Custom" existingResponse="Replace"> --><!--defaultPath="/Errors/NotFound"--><!--
  <remove statusCode="404" subStatusCode="-1"/>
  <remove statusCode="500" subStatusCode="-1"/>
  <error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL"/>
  <error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL"/>
</httpErrors> 

但问题是目前我在事件查看器及其相应的堆栈跟踪中看不到 500 和 404 错误

我已使用 Microsoft 应用程序洞察对 Application_Error 进行错误处理,但它不像事件查看器那样具有描述性

     protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();

        var httpStatusCode = (int)HttpStatusCode.InternalServerError;
        try
        {
            httpStatusCode = ((HttpException) exception).GetHttpCode();
        }
        catch (Exception)
        {
        }

        var dateTime = DateTime.Now.ToString();
        var ip = Request.ServerVariables["REMOTE_ADDR"];

        var userName = string.Empty;
        try
        {
            userName = HttpContext.Current.User.Identity.Name;
        }
        catch
        {
        }

       var url = string.Empty;
        if (HttpContext.Current != null)
        {
             url = HttpContext.Current.Request.Url.ToString();
        }


        var log = string.Format("Url: {0} , httpStatusCode: {1}, " +
                                "dateTime: {2}, exceptionMessage: {3}, " +
                                "ipAddress: {4}, userName: {5} ",
            url, httpStatusCode, dateTime, exception.Message, ip, userName);

        ServerAnalytics.CurrentRequest.LogEvent(log);

    }

我需要像事件查看器一样清楚地记录错误

我该怎么办?

还有哪些其他选择? newrelic,应用洞察?

【问题讨论】:

    标签: c# asp.net-mvc error-handling


    【解决方案1】:

    您没有在日志记录中使用异常堆栈跟踪。不要在错误日志中使用 exception.message,而是使用 exception.ToString(),它将包括堆栈跟踪。

    【讨论】:

    • 您是否在代码中的任何位置使用 Server.ClearError?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多