简单的异常处理--生成xml异常日志

来源:互联网 发布:应用多开软件 编辑:程序博客网 时间:2024/04/30 00:20
自己业余写了个简单的异常处理方法,是把异常信息按日期储存到xml日志文件中,方便分析。

主要实现的功能是:
1,根据日期创建xml日志文件,如2006-04-26log.xml
2,把每一个异常信息,包括一些环境信息储存到xml日志中
3,对外显示一个友好的错误信息

在一下小软件上还是有一些帮助的,尤其对于bug返回很有帮助,对方只要把xml文件反馈,我们就可以根据xml上的异常信息来修复一些bug

让我们先看看日志的效果,以下是实际产生的xml日志内容:
<root>
<ExceptionInformation>
<AdditionalInformationProperty>
  
<ExceptionManager.Title>WindowsApplication12</ExceptionManager.Title> 
  
<ExceptionManager.MachineName>GRAPECITY-TONY</ExceptionManager.MachineName> 
  
<ExceptionManager.TimeStamp>2006-4-26 21:35:22</ExceptionManager.TimeStamp> 
  
<ExceptionManager.FullName>WindowsApplication12, Version=1.0.2307.37434, Culture=neutral, PublicKeyToken=null</ExceptionManager.FullName> 
  
<ExceptionManager.AppDomainName>WindowsApplication12.exe</ExceptionManager.AppDomainName> 
  
<ExceptionManager.ThreadIdentity /> 
  
<ExceptionManager.WindowsIdentity>GRAPECITY-TONY/Tony</ExceptionManager.WindowsIdentity> 
  
</AdditionalInformationProperty>
<Exception>
  
<ExceptionType>System.NullReferenceException</ExceptionType> 
  
<Message>Object reference not set to an instance of an object.</Message> 
  
<TargetSite>Void Form1_Load(System.Object, System.EventArgs)</TargetSite> 
  
<StackTrace>at WindowsApplication12.Form1.Form1_Load(Object sender, EventArgs e) in C:/Documents and Settings/Tony/My Documents/Visual Studio Projects/WindowsApplication12/Form1.vb:line 48</StackTrace> 
  
<Source>WindowsApplication12</Source> 
  
</Exception>
  
</ExceptionInformation>
  
</root>

stackTrace中根据到了出错位置。
更近一步,我们可以创建一个xsl文件来把上述的xml文件格式化的更友好一些,这个下次有空在弄吧,先把异常处理类贴上来吧:
Imports System.Xml
Imports System.Collections.Specialized
Imports System.IO
Imports System.Security
Imports System.Threading
Imports System.Security.Principal


''' -----------------------------------------------------------------------------
'
'' Project     : WindowsApplication12
'
'' Class     : ExceptionManager
'
'' 
'
'' -----------------------------------------------------------------------------
'
'' <summary>
'
'' 异常管理类
'
'' </summary>
'
'' <remarks>
'
'' 根据日期,在指定xml文件中写入异常记录
'
'' </remarks>
'
'' <history>
'
''     [Tony]    2006-4-26    Created
'
'' </history>
'
'' -----------------------------------------------------------------------------
Public NotInheritable Class ExceptionManager

    
Private Shared EXCEPTIONMANAGER_NAME As String = GetType(ExceptionManager).Name
    
Private Shared EXCEPTIONMANAGEMENT_CONFIG_SECTION As String = "exceptionManagement"
    
Private Shared EXCEPTIONLOG_DIRECTORY As String = "Exception Log"
    
Private Shared RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED As String = "The event source {0} does not exist and cannot be created with the current permissions."
    
Private Shared RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION As String = "ExceptionManagerInternalException"
    
Private Shared EXCEPTIONMANAGER_ERROR_ON_SAVE = "The error log can not saved on the disk."


Constructor

Public Methods

Private Methods

End Class



里面注释已经写的挺清楚了,就不多加解释了。
现在看看我们怎么调用这个shared方法,我们可以在try.....catch.....代码中调用它,如下:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
Try
            
Dim a As String = Nothing
            
Dim b As Int16 = a.Length
        
Catch ex As Exception
            ExceptionManager.Publish(ex)
        
End Try
    
End Sub

运行该form1窗体,将提示如下错误提示窗体:


同时,程序的目录下,出现了“Exception Log”子目录,该目录下,自动生成了2006-04-26-log.xml这个日志文件。

xml日志文件的内容已经在上面列出过了。如果当日还有其他异常发生,2006-04-26-log.xml中将增加一个节点。

这个异常处理方法可能还有不少不足,不过我想简单应用应该可以了吧?我也会在今后逐渐改进他。也请朋友们多多提意见。谢谢。