One, design XML, should contain the following information:
1. DateTime: Date and time when error/exception occurred
2. File name: FileName with error/exception
3, class name: The names of the classes where the error/exception occurred
4. MethodName: The name of the method in which the error/exception occurred
5, Errormethod: The name of the function containing the error code
6, Message: Error/exception information
7. Error Details: Errors/anomalies Particulars
8, IP: Client IP Address
9, url: The URL where the error occurred
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>
Second, the design error processing class: ErrorHandler.cs, inside the Writeerror method needs exception and filename two parameters:
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 ();
}
}
}