一、设计xml,应该包含下面信息:
1、Datetime: 发生错误/异常的日期和时间
2、File name: 发生错误/异常的文件名
3、Class name : 发生错误/异常的类名
4、Methodname: 发生错误/异常的方法名
5、Errormethod : 包含错误代码的函数名字
6、Message :错误/异常的信息
7、Error details: 错误/异常详情
8、IP : 客户端IP地址
9、URL : 发生错误的URL
DemoXML:
DemoXML
<?
xml version="1.0" encoding="utf-8"
?>
< errorlog >
< error >
< datetime > datetime </ datetime >
< filename > filename </ filename >
< classname > classname </ classname >
< methodname > methodname </ methodname >
< errormethod > errormethod </ errormethod >
< messsage > ErrorMessage </ messsage >
< errordetails > Details goes here </ errordetails >
< IP > IP adress </ IP >
< url > URL </ url >
</ error >
</ errorlog >
< errorlog >
< error >
< datetime > datetime </ datetime >
< filename > filename </ filename >
< classname > classname </ classname >
< methodname > methodname </ methodname >
< errormethod > errormethod </ errormethod >
< messsage > ErrorMessage </ messsage >
< errordetails > Details goes here </ errordetails >
< IP > IP adress </ IP >
< url > URL </ url >
</ error >
</ errorlog >
二、设计错误处理类:errorHandler.cs,里面的WriteError方法需要Exception和FileName两个参数:
errorHandler.cs
using
System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
namespace code_center
{
public class errorHandler
{
string _strErrorMessage, _strDetails, _strClassName, _strMethodName;
DateTime _dtOccuranceTime = new DateTime();
public errorHandler()
{
}
public errorHandler(DateTime time, string className, string methodName,
string errorMessage, string details)
{
_dtOccuranceTime = time;
_strClassName = className;
_strDetails = details;
_strErrorMessage = errorMessage;
_strMethodName = methodName;
}
public static void WriteError(Exception ex)
{
WriteError(ex, "" );
}
public static void WriteError(Exception ex, string fileName)
{
XmlDocument doc = new XmlDocument();
string strRootPath =
System.Configuration.ConfigurationManager.AppSettings[ " logfilepath " ].ToString();
string xmlPath = System.Web.HttpContext.Current.Server.MapPath(strRootPath);
doc.Load(@xmlPath);
XmlNode newXMLNode, oldXMLNode;
oldXMLNode = doc.ChildNodes[ 1 ].ChildNodes[ 0 ];
newXMLNode = oldXMLNode.CloneNode( true );
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame( 1 );
MethodBase methodBase = stackFrame.GetMethod();
newXMLNode.ChildNodes[ 0 ].InnerText = DateTime.Now.ToString();
newXMLNode.ChildNodes[ 1 ].InnerText = fileName;
newXMLNode.ChildNodes[ 2 ].InnerText = methodBase.DeclaringType.FullName;
newXMLNode.ChildNodes[ 3 ].InnerText = methodBase.Name;
newXMLNode.ChildNodes[ 4 ].InnerText = ex.TargetSite.Name;
newXMLNode.ChildNodes[ 5 ].InnerText = ex.Message;
newXMLNode.ChildNodes[ 6 ].InnerText = ex.StackTrace;
newXMLNode.ChildNodes[ 7 ].InnerText = System.Web.HttpContext.Current.Request.UserHostAddress;
newXMLNode.ChildNodes[ 8 ].InnerText = System.Web.HttpContext.Current.Request.Url.OriginalString;
doc.ChildNodes[ 1 ].AppendChild(newXMLNode);
doc.Save(@xmlPath);
doc.RemoveAll();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
namespace code_center
{
public class errorHandler
{
string _strErrorMessage, _strDetails, _strClassName, _strMethodName;
DateTime _dtOccuranceTime = new DateTime();
public errorHandler()
{
}
public errorHandler(DateTime time, string className, string methodName,
string errorMessage, string details)
{
_dtOccuranceTime = time;
_strClassName = className;
_strDetails = details;
_strErrorMessage = errorMessage;
_strMethodName = methodName;
}
public static void WriteError(Exception ex)
{
WriteError(ex, "" );
}
public static void WriteError(Exception ex, string fileName)
{
XmlDocument doc = new XmlDocument();
string strRootPath =
System.Configuration.ConfigurationManager.AppSettings[ " logfilepath " ].ToString();
string xmlPath = System.Web.HttpContext.Current.Server.MapPath(strRootPath);
doc.Load(@xmlPath);
XmlNode newXMLNode, oldXMLNode;
oldXMLNode = doc.ChildNodes[ 1 ].ChildNodes[ 0 ];
newXMLNode = oldXMLNode.CloneNode( true );
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame( 1 );
MethodBase methodBase = stackFrame.GetMethod();
newXMLNode.ChildNodes[ 0 ].InnerText = DateTime.Now.ToString();
newXMLNode.ChildNodes[ 1 ].InnerText = fileName;
newXMLNode.ChildNodes[ 2 ].InnerText = methodBase.DeclaringType.FullName;
newXMLNode.ChildNodes[ 3 ].InnerText = methodBase.Name;
newXMLNode.ChildNodes[ 4 ].InnerText = ex.TargetSite.Name;
newXMLNode.ChildNodes[ 5 ].InnerText = ex.Message;
newXMLNode.ChildNodes[ 6 ].InnerText = ex.StackTrace;
newXMLNode.ChildNodes[ 7 ].InnerText = System.Web.HttpContext.Current.Request.UserHostAddress;
newXMLNode.ChildNodes[ 8 ].InnerText = System.Web.HttpContext.Current.Request.Url.OriginalString;
doc.ChildNodes[ 1 ].AppendChild(newXMLNode);
doc.Save(@xmlPath);
doc.RemoveAll();
}
}
}
三、 在Web.config加入:
<
appSettings
>
< add key ="logfilepath" value ="~/errorHandling/errorlog.xml" />
</ appSettings >
< add key ="logfilepath" value ="~/errorHandling/errorlog.xml" />
</ appSettings >
四、测试:在你的网站中加入两段测试代码
1、在Page_Load:
Page_Load
public
partial
class
_Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
try
{
throw new Exception( " Custom error " );
}
catch (Exception ex)
{
Response.Write(ex.Message);
kirin.errorHandler.WriteError(ex, " Default.aspx.cs " );
}
}
}
{
protected void Page_Load( object sender, EventArgs e)
{
try
{
throw new Exception( " Custom error " );
}
catch (Exception ex)
{
Response.Write(ex.Message);
kirin.errorHandler.WriteError(ex, " Default.aspx.cs " );
}
}
}
2、在Application_Error中加入:
void
Application_Error(
object
sender, EventArgs e)
{
code_center.errorHandler.WriteError(Server.GetLastError().GetBaseException(),
" Global.asax " );
}
{
code_center.errorHandler.WriteError(Server.GetLastError().GetBaseException(),
" Global.asax " );
}
3、Page_Load异常的结果:
errorXML
<
error
>
< datetime > 2010-1-29 9:29:24 </ datetime >
< filename > Default.aspx.vb </ filename >
< classname > kirin._Default </ classname >
< methodname > Page_Load </ methodname >
< errormethod > Page_Load </ errormethod >
< messsage > Custom error </ messsage >
< errordetails > 在 kirin._Default.Page_Load(Object sender, EventArgs e) 位置 C:\Demo\kirin_code_center\kirin\Default.aspx.cs:行号 16 </ errordetails >
< IP > 127.0.0.1 </ IP >
< url > http://localhost:2192/default.aspx </ url >
</ error >
< datetime > 2010-1-29 9:29:24 </ datetime >
< filename > Default.aspx.vb </ filename >
< classname > kirin._Default </ classname >
< methodname > Page_Load </ methodname >
< errormethod > Page_Load </ errormethod >
< messsage > Custom error </ messsage >
< errordetails > 在 kirin._Default.Page_Load(Object sender, EventArgs e) 位置 C:\Demo\kirin_code_center\kirin\Default.aspx.cs:行号 16 </ errordetails >
< IP > 127.0.0.1 </ IP >
< url > http://localhost:2192/default.aspx </ url >
</ error >
五、完整代码:/Files/zhuqil/kirin_errorHandler.rar
本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2010/01/29/error_tracking.html,如需转载请自行联系原作者