全局 asax 中的最佳实践错误处理
本文关键字:错误 处理 最佳 asax 全局 | 更新日期: 2023-09-27 17:56:38
我现在在全局 asax 中有下面的代码,我想将异常日志存储在数据库中,这是好的做法吗? 因为如果那里发生SQL错误,我也想记录它。所以我正在考虑更改下面的代码以编写文本日志而不是电子邮件,然后在 sql 错误时写入文本日志。
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
string testEnvironment = ConfigurationSettings.AppSettings["isTestEnvironment"];
if (testEnvironment == "0")
{
Exception ex = Server.GetLastError();
if (ex is HttpException && ex.InnerException is ViewStateException)
{
Response.Redirect(Request.Url.AbsoluteUri)
return
}
StringBuilder theBody = new StringBuilder();
theBody.Append("URL: " + Request.Url + "'n");
theBody.Append("Referer: " + Request.ServerVariables["HTTP_REFERER"] + "'n");
theBody.Append("IP: " + Request.ServerVariables["REMOTE_HOST"] + "'n");
theBody.Append("Error Message: " + ex.ToString() + "'n");
if (User.Identity.IsAuthenticated)
theBody.Append("User: " + User.Identity.Name + "'n");
else
theBody.Append("User is not logged in." + "'n");
theBody.Append("Form Values: " + "'n");
foreach (string s in Request.Form.AllKeys)
{
if (s != "__VIEWSTATE")
theBody.Append(s + ":" + Request.Form[s] + "'n");
}
theBody.Append("Session Values: " + "'n");
foreach (string s in Session.Keys)
theBody.Append(s + ":" + Session[s] + "'n");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.IsBodyHtml = false;
email.From = new System.Net.Mail.MailAddress("errors@karpach.com", "ErrorManager");
email.To.Add(new System.Net.Mail.MailAddress("errornotification@karpach.com", "Developer"));
email.Subject = Request.Url.ToString().Split('/')[2] + " has ASP.NET error";
email.Body = theBody.ToString();
try
{
System.Net.Mail.SmtpClient emailProvider = new System.Net.Mail.SmtpClient();
emailProvider.Send(email);
}
catch (Exception anException)
{
}
finally
{
if (Request.Url.Segments[Request.Url.Segments.Length - 1].ToLower() != "error.aspx")
Response.Redirect("~/error.aspx?msg=4");
else
{
Response.Write(@"We encountered an internal error. We apologize for any inconvenience
but know that our staff gets emailed EVERY error that occurs so that we can solve it promptly.");
Response.End();
}
}
}
}
您可以将其记录到 EventLog 中。系统管理员可以设置监视以查看何时添加新事件,并在潜在问题时收到警报。
下面是 MSDN 中用于 EventLog 类的示例:
if(!EventLog.SourceExists("MySource"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");