簡體   English   中英

如果應用程序崩潰,是否可以配置將哪些信息存儲在Windows事件日志中?

[英]Is it possible to configure what information gets stored in the Windows event log in the event of an application crash?

有時,錯誤會觸發那些不可恢復的異常之一(例如StackOverflowException ),導致我們整個ASP.NET MVC應用程序崩潰。 發生這種情況時,Windows事件日志通常包含一些有關該錯誤的信息,盡管該信息很少。 例如,當前,我們認為反射調用中引發了StackOverflowException 在這種情況下,事件日志包含有關外部TargetInvocationException一些基本信息,而沒有任何內容可以讓我們查明問題所在。 我的問題是,是否可能在.NET / IIS / Windows中配置某些內容以在發生這種情況時記錄更多信息? 例如,如果我們可以獲得完整異常的前N個字符(包括堆棧跟蹤和內部異常),則我們可以輕松調試並解決該問題。

您無法控制其他類寫入日志的內容,但是可以捕獲global.asax文件中的異常並進行自己的日志記錄。 我傾向於結合使用數據庫日志記錄和電子郵件通知。

--global.asax

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        Exception exSvr = Server.GetLastError();

        if (exSvr == null)
            return;

        if (exSvr.Message == "The client disconnected.")
        {
            // I don't want to be notified everytime someone closes
            //   the browser mid-request.
            return;
        }

        if (exSvr is System.Threading.ThreadAbortException || exSvr.InnerException is System.Threading.ThreadAbortException || exSvr.Message.StartsWith("Thread was being aborted") || (exSvr.InnerException != null && exSvr.InnerException.Message.StartsWith("Thread was being aborted")))
        {
            // So we don't get notified everytime .NET kills a thread.
            // NOTE:  This error is how .NET kills a thread, and will happen every time we call Response.Redirect("", true)
            return;
        }

        ErrorHandler.SendServerErrMsg("Application Error", exSvr, HttpContext.Current);
    }

--ErrorHandler

    public static void SendServerErrMsg(string codeSection, Exception exSvr, HttpContext context, SeverityLevel lvl = SeverityLevel.Warning)
    {
        if (_config == null || !_config.EnableEmailAlerts)
            return;

        if (context != null && context.Request != null && context.Request.Url != null && context.Request.Url.IsLoopback && !_config.SendAlertsInDebugMode)
            return;

        if (context != null && context.IsDebuggingEnabled && !_config.SendAlertsInDebugMode)
            return;

        // Attempt to get the currently logged in user.
        System.Web.Security.MembershipUser curUsr = null;
        try
        { curUsr = System.Web.Security.Membership.GetUser(); }
        catch { } // If it fails, don't worry about it.

        // Set the message's subject line.
        string subject = _config.ApplicationName + " Exception - " + HttpUtility.HtmlEncode(codeSection);

        // Build the page body in a StringBuilder object.
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        if (context != null && context.Request != null)
            sb.AppendLine(string.Format("<h2>{0}</h2>", HttpUtility.HtmlEncode(context.Request.ApplicationPath)));
        sb.AppendLine("<p>The following exception has occurred:</p>");
        sb.AppendLine("<ul>");
        Exception innerEx = exSvr;
        while (innerEx != null)
        {
            sb.AppendLine(string.Format("<li>{0}</li>", HttpUtility.HtmlEncode(innerEx.Message)));
            innerEx = innerEx.InnerException;
        }
        sb.AppendLine("</ul>");
        sb.AppendLine("<p/>");
        if (exSvr.TargetSite != null)
            sb.AppendLine(string.Format("<p>Target Site: {0} {1} (in {2})</p>", exSvr.TargetSite.MemberType, exSvr.TargetSite.Name, exSvr.TargetSite.DeclaringType));
        else
            sb.AppendLine("<p>Target Site: N/A</p>");
        sb.AppendLine("<p/>");
        sb.AppendLine("<hr />");
        if (context != null && context.Request != null)
        {
            HttpRequest req = context.Request;
            sb.AppendLine("<p>");
            sb.AppendLine(string.Format("<div>Request URL: {0}</div>", HttpUtility.HtmlEncode(req.Url.ToString())));
            sb.AppendLine(string.Format("<div>Request Path: {0}</div>", HttpUtility.HtmlEncode(req.FilePath)));
            sb.AppendLine(string.Format("<div>User: {0}</div>", (curUsr != null) ? HttpUtility.HtmlEncode(curUsr.UserName) : "Unknown"));
            sb.AppendLine(string.Format("<div>User Host Address: {0}</div>", HttpUtility.HtmlEncode(req.UserHostAddress)));
            // We're going to try a reverse DNS search on the IP address.  Failover,
            //   just uses the UserHostName value from the HttpContext object.
            string hostName = null;
            try { hostName = System.Net.Dns.GetHostEntry(req.UserHostAddress).HostName; }
            catch { hostName = null; }
            if (hostName == null) hostName = req.UserHostName;
            sb.AppendLine(string.Format("<div>User Host Name: {0}</div>", HttpUtility.HtmlEncode(hostName)));
            sb.AppendLine(string.Format("<div>User Agent: {0}</div>", HttpUtility.HtmlEncode(req.UserAgent)));
            sb.AppendLine(string.Format("<div>Server Name: {0}</div>", (context.Server != null ? HttpUtility.HtmlEncode(context.Server.MachineName) : "Unknown")));
            sb.AppendLine(string.Format("<div>Request Identity: {0}</div>", (req.LogonUserIdentity != null ? HttpUtility.HtmlEncode(req.LogonUserIdentity.Name) : "Unknown")));

            sb.AppendLine("</p>");
            sb.AppendLine("<hr />");
        }
        sb.AppendLine("<p>Stack trace follows:</p>");
        Exception ex = exSvr;
        while (ex != null)
        {
            sb.AppendLine("<p>");
            sb.AppendLine(string.Format("<b>&gt;&gt;&nbsp;{0}:</b> {1} in {2}<br/>", ex.GetType().Name, HttpUtility.HtmlEncode(ex.Message), HttpUtility.HtmlEncode(ex.Source)));
            sb.AppendLine(HttpUtility.HtmlEncode(ex.StackTrace));
            sb.AppendLine("</p>");
            ex = ex.InnerException;
        }
        if (context!=null && context.Trace.IsEnabled)
        {
            sb.AppendLine("<hr />");
            sb.AppendLine("<p>Web Trace:</p>");
            sb.AppendLine(string.Format("<p>{0}</p>", HttpUtility.HtmlEncode(context.Trace.ToString())));
        }

        EmailHelper.SendEmail(GetAlertContacts(), subject.Trim(), sb.ToString().Trim(), true);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM