有大量资源显示如何使用global.ascx全局错误事件处理程序来捕获Web应用程序中的未管理错误,但我还没有找到要在此方法中包含的代码的良好示例主要主题,如错误消息,堆栈跟踪的输出,生成错误的页面,生成错误的用户名/角色......等等。
有没有人为此目的使用,拥有或遇到过一个不错的代码专家?
提前感谢。
答案 0 :(得分:1)
你最好什么都不做。 ASP.NET Health Monitoring会将事件写入事件日志,其中包含您可能需要的大部分数据。
如果这对您来说不够,那么您应该考虑将一些异常包含在包含附加数据的外部异常中。例如:
string fileSpec = " ... ";
try
{
using (var stream = new FileStream(fileSpec))
{
// Something
}
}
catch (IOException ex)
{
throw new Exception(String.Format("Error processing {0}", fileSpec), ex);
}
答案 1 :(得分:1)
以下是我们正在使用的内容,尽管我们现在可能希望切换到Health Monitoring,查看此问题的其他答案。
public static void LogErrorWithStackTrace(String message, Exception exception,
EventLogEntryType entryType)
{
var context = HttpContext.Current;
var session = context.Session;
var request = context.Request;
var errorMessage =
String.Format(
@"Message: {0}
Error in: {1}
Referer: {2}
Agent: {3}
IP: {4}
Request type: {5}",
message, request.Url, request.UrlReferrer,
request.UserAgent, request.UserHostAddress, request.RequestType);
errorMessage += "\rQuery string variables:";
errorMessage = request.QueryString.Keys.Cast<string>().Aggregate(errorMessage,
(current, key) =>
current +
"\r " + key + " = " +
request.QueryString[
key]);
errorMessage += "\rForm variables:";
errorMessage = request.Form.Keys.Cast<string>().Aggregate(errorMessage,
(current, key) =>
current + "\r " + key +
" = " +
request.Form[key]);
errorMessage += "\rCookies:";
errorMessage = request.Cookies.Keys.Cast<string>().Aggregate(errorMessage,
(current, key) =>
current + "\r " + key +
" = " +
request.Cookies[key]);
if (session != null)
{
errorMessage += "\rISession:";
var sess = (ISession) session["session"];
errorMessage += sess.ToString();
errorMessage += "\rSession:";
errorMessage = session.Keys.Cast<string>().Aggregate(errorMessage,
(current, sessionKey) =>
current +
"\r " + sessionKey + ": " +
session[sessionKey]);
}
errorMessage += "\rStack Trace:";
errorMessage += exception.StackTrace;
WriteEntry(errorMessage, entryType);
if (!QuietTypes.Contains(exception.GetType()))
SendExceptionEmail(errorMessage);
}
答案 2 :(得分:0)
这是我们使用的一些VB代码,它为我们诊断大多数问题提供了充分的信息。这种电子邮件错误的唯一缺点是当出现重大故障时,您最终可能会在邮箱中收到数千封邮件:(
Dim e As Exception
If System.Web.HttpContext.Current.Request.IsLocal Then Exit Sub
e = System.Web.HttpContext.Current.Server.GetLastError
If e IsNot Nothing Then
If TypeOf e Is HttpException Then
Dim he As HttpException = DirectCast(e, HttpException)
If he.GetHttpCode=404 Then
System.Web.HttpContext.Current.Server.ClearError()
Response.Redirect("/")
Exit Sub
End If
End If
MsgBody += "Exception: " & e.Message & vbCrLf & vbCrLf
MsgBody += e.ToString & vbCrLf & vbCrLf & vbCrLf
Else
MsgBody += "Unknown Error" & vbCrLf & vbCrLf & vbCrLf
End If
If System.Web.HttpContext.Current.Request.Url Is Nothing Then
MsgBody += "URL: ?" & vbCrLf & vbCrLf
Else
MsgBody += "URL: " & System.Web.HttpContext.Current.Request.Url.ToString & vbCrLf
If System.Web.HttpContext.Current.Request.RawUrl IsNot Nothing Then MsgBody += System.Web.HttpContext.Current.Request.RawUrl & vbCrLf
MsgBody += vbCrLf
End If
If System.Web.HttpContext.Current.Request.UrlReferrer Is Nothing Then
MsgBody += "Referer: <direct>" & vbCrLf & vbCrLf
Else
MsgBody += "Referer: " & System.Web.HttpContext.Current.Request.UrlReferrer.ToString() & vbCrLf & vbCrLf
End If
If User IsNot Nothing Then MsgBody += "User: " & User.Identity.Name & vbCrLf
MsgBody += "User-Agent: " & System.Web.HttpContext.Current.Request.UserAgent & vbCrLf
MsgBody += "User-IP: " & System.Web.HttpContext.Current.Request.UserHostAddress & vbCrLf
MsgBody += "Server: " & System.Environment.MachineName & vbCrLf
MsgBody += vbCrLf & "RequestType: " & System.Web.HttpContext.Current.Request.RequestType() & vbCrLf & vbCrLf
' dump request items
' QueryString, Form, Cookies, ServerVariables
MsgBody += "Querystring Variables ================" & vbCrLf
If Request.QueryString.Count > 0 Then
For Each key As String In Request.QueryString.Keys
MsgBody += key & " = " & Request.QueryString(key) & vbCrLf
Next
Else
MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf
MsgBody += "Form Variables =======================" & vbCrLf
If Request.Form.Count > 0 Then
For Each key As String In Request.Form.Keys
MsgBody += key & " = " & Request.Form(key) & vbCrLf
Next
Else
MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf
MsgBody += "Cookies ==============================" & vbCrLf
If Request.Cookies.Count > 0 Then
For Each key As String In Request.Cookies.Keys
MsgBody += key & " = " & Request.Cookies(key).Value & vbCrLf
Next
Else
MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf
MsgBody += "ServerVariables ======================" & vbCrLf
If Request.ServerVariables.Count > 0 Then
For Each key As String In Request.ServerVariables.Keys
MsgBody += key & " = " & Request.ServerVariables(key) & vbCrLf
Next
Else
MsgBody += "(none)" & vbCrLf
End If
MsgBody += vbCrLf
' we send MsgBody via email using the System.Net.Mail.MailMessage & System.Net.Mail.SmtpClient classes
' we've handled the error
System.Web.HttpContext.Current.Server.ClearError()
' depending on the error we redirect to our homepage /default.aspx unless that is the faulting page then redirect to static /error.html