我有处理应用程序错误的代码(可以在互联网上找到)。我在事件日志中写道。
void Application_Error(object sender, EventArgs e)
{
Exception myError = null;
if (HttpContext.Current.Server.GetLastError() != null)
{
string eventLog = "MySite";
string eventSource = "www.mysite.com";
string myErrorMessage = "";
myError = Server.GetLastError();
while (myError.InnerException != null)
{
myErrorMessage += "Messagern" +
myError.Message.ToString() + "rnrn";
myErrorMessage += "Sourcern" +
myError.Source + "rnrn";
myErrorMessage += "Target sitern" +
myError.TargetSite.ToString() + "rnrn";
myErrorMessage += "Stack tracern" +
myError.StackTrace + "rnrn";
myErrorMessage += "ToString()rnrn" +
myError.ToString();
myError = myError.InnerException;
}
if (EventLog.SourceExists(eventSource))
{
EventLog myLog = new EventLog(eventLog);
myLog.Source = eventSource;
myLog.WriteEntry("An error occurred in the Web application "
+ eventSource + "rnrn" + myErrorMessage,
EventLogEntryType.Error);
}
}
}
这是来自事件日志的行:
Type Date Time Source Event Category
Error 03.04.2012 16:44:41 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
Error 03.04.2012 16:43:31 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
Error 03.04.2012 16:42:56 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
Error 03.04.2012 16:42:56 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
Error 03.04.2012 16:42:54 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
Error 03.04.2012 16:37:27 www.mysite.com 0 "An error occurred in the Web application www.mysite.com
"
正如您所指出的,错误大约每秒发生一次。但是关于一个错误的信息是空的
这个代码出了什么问题
谢谢
问题出在这一行imo
while (myError.InnerException != null)
只有当存在内部异常时,才可以编写错误的详细信息,但这并不总是真正的
你还忘了打
Server.ClearError();
在处理结束时(但我想这是一个选择)