using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Xml;using System.Web.SessionState;using System.Data;using System.Configuration;using System.Collections;namespace ynet.cba.WebLogic.YongDian...{ public class errorlog ...{ private static errorlog _Instance = null; private static string strRootName; private static XmlElement xmlRoot; private static XmlDocument xmlDoc; private string errorfile = ((Hashtable)ConfigurationManager.GetSection("errorfile"))["setting1"].ToString(); public errorlog() ...{ Load(); } private bool Load() ...{ if (!File.Exists(errorfile)) CreateErrorLogFile(errorfile); if (xmlDoc == null) xmlDoc = new XmlDocument(); try ...{ xmlDoc.Load(errorfile); } catch ...{ return false; } xmlRoot = xmlDoc.DocumentElement; if (xmlRoot != null) strRootName = "/" + xmlRoot.Name + "/"; return true; } private void CreateErrorLogFile(string strFileName) ...{ StringBuilder sb = new StringBuilder(); sb.Append("<?xml version='1.0' ?> "); sb.Append("<ERRORLOG>"); sb.Append("</ERRORLOG>"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sb.ToString()); xmlDoc.Save(strFileName); } private void AddXmlAttribute(XmlElement xNode, string strAttr, string strAttrvalue) ...{ XmlAttribute xAttr = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, strAttr, null); xAttr.InnerText = strAttrvalue; xNode.Attributes.Append(xAttr); } public void SetErrorLog(ErrorLogItem errItem) ...{ XmlElement xErrorElement = xmlDoc.CreateElement("error"); AddXmlAttribute(xErrorElement, "username", errItem.User); AddXmlAttribute(xErrorElement, "application", errItem.AppName); AddXmlAttribute(xErrorElement, "classname", errItem.ClassName); AddXmlAttribute(xErrorElement, "functionname", errItem.FunctionName); AddXmlAttribute(xErrorElement, "position", errItem.Position); AddXmlAttribute(xErrorElement, "occurtime", errItem.OccurTime.ToString("g")); xErrorElement.InnerText = errItem.ErrorInfo; xmlRoot.AppendChild(xErrorElement); if (xmlRoot.ChildNodes.Count > 1000) xmlRoot.RemoveChild(xmlRoot.FirstChild); xmlDoc.Save(errorfile); } public static errorlog Instance() ...{ if(_Instance == null) ...{ _Instance = new errorlog(); } return _Instance ; } public static void LogAppError(string thisErr, string strClass, string strFunc, string strPos, string strUser) ...{ ErrorLogItem errItem = new ErrorLogItem(); errItem.AppName = "远能电力抄表管理系统"; errItem.ClassName = strClass; errItem.ErrorInfo = thisErr; errItem.FunctionName = strFunc; errItem.OccurTime = DateTime.Now; errItem.Position = strPos; errItem.User = strUser; try ...{ errorlog.Instance().SetErrorLog(errItem); //SendErrorLogMail(ErrorLogEmail, sb.ToString()); } catch(Exception ee ) ...{ throw new Exception("An error occur :"+ ee.Message); } finally ...{ } } } public struct ErrorLogItem ...{ public string User;//当前登录人 public string AppName;//应用程序名称 public string ClassName;//错误发生的类名称 public string FunctionName;//错误发生的方法(事件)名称 public string Position;//错误的位置(或其它信息) public string ErrorInfo;//错误信息 public DateTime OccurTime;//错误发生的时间 } }在web.config中的配置<?xml version="1.0" encoding="utf-8"?><CONFIGURATION> <CONFIGSECTIONS> <SECTION name="errorfile" type="System.Configuration.SingleTagSectionHandler" /> <SECTIONGROUP name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <SECTIONGROUP name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <SECTION name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" /> <SECTIONGROUP name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <SECTION name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="Everywhere" requirePermission="false" /> <SECTION name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" /> <SECTION name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" /> </SECTIONGROUP> </SECTIONGROUP> </SECTIONGROUP> </CONFIGSECTIONS> <ERRORFILE setting1="C:errorlog.xml" /> <CONNECTIONSTRINGS> <ADD name="ynetcbaConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=SoftDev;Initial Catalog=ynet.cba;Persist Security Info=True;User ID=sa;Password=8888" /> </CONNECTIONSTRINGS>