错误处理类

本文介绍了一个使用C#实现的错误日志记录系统。该系统通过XML文件存储错误信息,并利用配置文件设置错误日志文件路径。文章详细展示了如何创建错误日志类、结构体及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 &gt; 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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值