1. Design xml, which should contain the following information:
1. Datetime: The date and time when the error/exception occurred
2. File name: The file name where the error/exception occurred
3. Class name: the class name where the error/exception occurred
4. Methodname: The name of the method where the error/exception occurred
5. Errormethod: function name containing error code
6. Message: error/exception information
7. Error details: Error/exception details
8. IP: Client IP address
9. URL: URL where the error occurred
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>
2. Design the error handling class: errorHandler.cs. The WriteError method inside requires two parameters: Exception and 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();
}
}
}
3. Add in Web.config:
<appSettings>
<add key="logfilepath" value="~/errorHandling/errorlog.xml"/>
</appSettings>
4. Test: Add two pieces of test code to your website
1. In 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");
}
}
}
2. Add: to Application_Error:
void Application_Error(object sender, EventArgs e)
{
code_center.errorHandler.WriteError(Server.GetLastError().GetBaseException(),
"Global.asax");
}
3. Page_Load exception results:
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> In kirin._Default.Page_Load(Object sender, EventArgs e) location C:Demokirin_code_centerkirinDefault.aspx.cs: line number 16</errordetails>
<IP>127.0.0.1</IP>
<url>http://localhost:2192/default.aspx</url>
</error>
5. Complete code:/Files/zhuqil/kirin_errorHandler.rar
Author: Zhu Qilin Source: http://zhuqil.cnblogs.com
The copyright of this article belongs to the author and Blog Park. Reprinting is welcome. However, this statement must be retained without the author's consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, the right to pursue legal liability is reserved.