使用Microsoft.ApplicationBlocks.ExceptionManagement处理程序异常

详细文档看:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/emab-rm.asp
说明几点:
文件夹:Microsoft.ApplicationBlocks.ExceptionManagement
为异常主类文件.
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces
为异常接口类文件,
安装
1.使用installutil安装DLL文件,
安装方法,
Visual Studio .NET 2003 命令提示+installutil Microsoft.ApplicationBlocks.ExceptionManagement.dll所有路径
如:installutil E:\exception\Microsoft.ApplicationBlocks.ExceptionManagement\bin\Debug\Microsoft.ApplicationBlocks.ExceptionManagement.dll
程序使用:
添加对Microsoft.ApplicationBlocks.ExceptionManagement.dll引用,
在配制文件添加配制

None.gif   < configSections >
None.gif            
< section  name ="exceptionManagement"  type ="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft.ApplicationBlocks.ExceptionManagement"   />
None.gif        
</ configSections >
None.gif        
< exceptionManagement  mode ="on" >     
                <!--使用自定义数据记录使用下面配制-->
None.gif            <!--
< publisher  mode ="on"  
None.gif                assembly
="Microsoft.ApplicationBlocks.ExceptionManagement"   
None.gif                type
="Edobnet.FrameWork.ExceptionHandler.ExceptionDBPublisher"
None.gif                connString
="USER ID=sa;PASSWORD=232323;INITIAL CATALOG=Errors;DATA SOURCE=localhost"   />-->
None.gif         <!--使用自定义数据记录使用下面配制-->   
            <!--< publisher  mode ="on"  
None.gif                    assembly
="Microsoft.ApplicationBlocks.ExceptionManagement"   
None.gif                    type
="Edobnet.FrameWork.ExceptionHandler.ExceptionLogPublisher"  
None.gif                    fileName
="philips.log"
None.gif                    filePath
="C:"
None.gif                    stackTrace 
= "true"
None.gif                    daily
="true"   />
None.gif                    -->
None.gif        
</ exceptionManagement >

添加USING
using Microsoft.ApplicationBlocks.ExceptionManagement;

测试代码:

None.gif try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
int t = 0;
InBlock.gif                
int i = 1/t;
ExpandedBlockEnd.gif            }

None.gif            
catch (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                ExceptionManager.Publish( ex );
InBlock.gif
ExpandedBlockEnd.gif            }

如果不使用自己定议异常处理,只会在WINDOWS事件里记录,

自定义异常处理:

None.gif
None.gif
using  System;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Specialized;
None.gif
using  Microsoft.ApplicationBlocks.ExceptionManagement;
None.gif
None.gif
namespace  Edobnet.FrameWork.ExceptionHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// <para>
InBlock.gif    
/// This ExceptionDBPublisher class is a custom publisher for the
InBlock.gif    
/// Microsoft Exception Management Application Block.
InBlock.gif    
/// </para><para>
InBlock.gif    
/// It writes Exception info to a database including
InBlock.gif    
/// Message, Source and Stack Trace.
InBlock.gif    
/// </para>
InBlock.gif    
/// </summary>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// <para>This class is a custom publisher for the
InBlock.gif    
/// Microsoft Exception Management Application Block.
InBlock.gif    
/// Note that the Stored Procedure spLogError and table
InBlock.gif    
/// ErrorLog must exist in the database.
InBlock.gif    
/// It writes Exception info to a database including:
InBlock.gif    
/// <list type="bullet">
InBlock.gif    
/// <item><term>Message</term>
InBlock.gif    
/// <description>Exception Message</description></item>
InBlock.gif    
/// <item><term>Source</term>
InBlock.gif    
/// <description>The Exception source</description></item>
InBlock.gif    
/// <item><term>StackTrace</term>
InBlock.gif    
/// <description>The Stack Trace</description></item></list>
InBlock.gif    
/// </para>
InBlock.gif    
/// <para>
InBlock.gif    
/// Configuration Parameters and examples:
InBlock.gif    
/// <list type="bullet">
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>assembly</term>
InBlock.gif    
/// <description>assembly file name: assembly="[Dll name without '.dll']"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>type</term>
InBlock.gif    
/// <description>class and namespace: type="[Fully qualified namespace and type]"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>connString</term>
InBlock.gif    
/// <description>Connection String: connString="db_connect_string"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// </list>
InBlock.gif    
/// </para>
InBlock.gif    
/// <para>
InBlock.gif    
///    For more information see documentation on MS Exception Management
InBlock.gif    
///    Application Blocks</para>
InBlock.gif    
///    <example>Here is an example entry under &lt;exceptionManagement&gt;"
InBlock.gif    
///    <code>
InBlock.gif    
///    &lt;publisher mode="on" 
InBlock.gif    
/// assembly="Com.Daveranck.ExceptionHandler"  
InBlock.gif    
/// type="Com.Daveranck.ExceptionHandler.ExceptionDBPublisher"
InBlock.gif    
/// connString="server=[Server Name];Integrated Security=SSPI;Database=[Database]" /&gt;
InBlock.gif    
///    </code>
InBlock.gif    
///    </example>
ExpandedSubBlockEnd.gif    
///    </remarks>

InBlock.gif    public class ExceptionDBPublisher : IExceptionPublisher
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The database connection string must be set in config file
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        // MUST be set in config file 
InBlock.gif
        private string m_ConnString = @"";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Constructor
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ExceptionDBPublisher()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Sets up the config settings from the app
InBlock.gif        
/// config file, if available. Otherewise,
InBlock.gif        
/// defaults are used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void SetConfig(NameValueCollection configSettings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Load Config values if they are provided.
InBlock.gif
            if (configSettings != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (configSettings["connString"!= null &&  
InBlock.gif                    configSettings[
"connString"].Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{  
InBlock.gif                    m_ConnString 
= configSettings["connString"];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Provide implementation of the IExceptionPublisher interface
InBlock.gif        
/// This contains the single Publish method.
InBlock.gif        
/// <param name="exception">Exception</param>
InBlock.gif        
/// <param name="additionalInfo">NameValuecollection</param>
InBlock.gif        
/// <param name="configSettings">NameValuecollection</param>
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        void IExceptionPublisher.Publish(Exception exception, 
InBlock.gif            NameValueCollection additionalInfo, 
InBlock.gif            NameValueCollection configSettings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Load config settings if available
InBlock.gif
            SetConfig(configSettings);
InBlock.gif
InBlock.gif            
// Create StringBuilder to maintain publishing information.
InBlock.gif
            StringBuilder strInfo = new StringBuilder();
InBlock.gif
InBlock.gif            
// Record the contents of the additionalInfo collection.
InBlock.gif
            if (additionalInfo != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Record General information.
InBlock.gif
                strInfo.AppendFormat("{0}General Information {0}", Environment.NewLine);
InBlock.gif                strInfo.AppendFormat(
"{0}Additonal Info:", Environment.NewLine);
InBlock.gif                
foreach (string i in additionalInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strInfo.AppendFormat(
"{0}{1}: {2}", Environment.NewLine, i, 
InBlock.gif                        additionalInfo.Get(i));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
// Log to database
InBlock.gif
            LogInDatabase(strInfo.ToString(),
InBlock.gif                exception.Message,
InBlock.gif                exception.Source,
InBlock.gif                exception.StackTrace);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// <para>This method will publish the Exception Message,
InBlock.gif        
/// Source and Stack Trace to a database.</para>
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Info">General exception info</param>
InBlock.gif        
/// <param name="Message">Excaption Mesage</param>
InBlock.gif        
/// <param name="Source">Exception Source</param>
ExpandedSubBlockEnd.gif        
/// <param name="StackTrace">Exception Stack Trace</param>

InBlock.gif        private void LogInDatabase(string Info, 
InBlock.gif            
string Message,
InBlock.gif            
string Source,
InBlock.gif            
string StackTrace)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Create connection
InBlock.gif
            SqlConnection sqlConn = new SqlConnection();
InBlock.gif            sqlConn.ConnectionString 
= m_ConnString;
InBlock.gif
InBlock.gif            
string spName = "spLogError";
InBlock.gif
InBlock.gif            SqlCommand sqlCmd 
= new SqlCommand(spName, sqlConn);
InBlock.gif            sqlCmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif            
// Add params
InBlock.gif
            SqlParameter pInfo = sqlCmd.Parameters.Add("@Info",    SqlDbType.VarChar, 500);
InBlock.gif            pInfo.Value 
= Info;
InBlock.gif            SqlParameter pMessage 
= sqlCmd.Parameters.Add("@Message",    SqlDbType.VarChar, 500);
InBlock.gif            pMessage.Value 
= Message;
InBlock.gif            SqlParameter pSource 
= sqlCmd.Parameters.Add("@Source",    SqlDbType.VarChar, 255);
InBlock.gif            pSource.Value 
= Source;
InBlock.gif            SqlParameter pStackTrace 
= sqlCmd.Parameters.Add("@StackTrace",    SqlDbType.VarChar, 1000);
InBlock.gif            pStackTrace.Value 
= StackTrace;
InBlock.gif
InBlock.gif            
// Log error
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlConn.Open();
InBlock.gif                
int result = sqlCmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Database problem rethrow exception
InBlock.gif                
// Exception Manager will log original exception
InBlock.gif                
// and this exception to Event log
InBlock.gif
                throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Clean up
InBlock.gif
                if (sqlConn.State != System.Data.ConnectionState.Closed &&
InBlock.gif                    sqlConn.State 
!= System.Data.ConnectionState.Broken)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sqlConn.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// <para>The ExceptionLogPublisher class is a custom publisher for the
InBlock.gif    
/// Microsoft Exception Management Application Block.
InBlock.gif    
/// </para><para>
InBlock.gif    
/// It writes exception info to a logfile including:
InBlock.gif    
/// Message and optionally Source and StackTrace.
InBlock.gif    
/// </para>
InBlock.gif    
/// </summary>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// <para>Configuration Parameters and examples:
InBlock.gif    
/// <list type="bullet">
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>assembly</term>
InBlock.gif    
/// <description>assembly file name: assembly="[Dll name without '.dll']"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>type</term>
InBlock.gif    
/// <description>class and namespace: type="[Fully qualified namespace and type]"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>fileName</term>
InBlock.gif    
/// <description>log file name: "myLogFile.txt"</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>filePath</term>
InBlock.gif    
/// <description>path to log file location (including trailing ""): "c:logs</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>stackTrace</term>
InBlock.gif    
/// <description>true or false - if true, stack trace included in log</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// <item>
InBlock.gif    
/// <term>daily</term>
InBlock.gif    
/// <description>true or false - if true, new log created each day and date 
InBlock.gif    
/// preppended to the fileName Ex: 20031114myLogFile.log</description>
InBlock.gif    
/// </item>
InBlock.gif    
/// </list> 
InBlock.gif    
///    <para>For more information see documentation on MS Exception Management
InBlock.gif    
///    Application Blocks</para>
InBlock.gif    
///    </para>
InBlock.gif    
///    <example>Here is an example entry under &lt;exceptionManagement&gt;
InBlock.gif    
///    <code>
InBlock.gif    
///    &lt;publisher mode="on" 
InBlock.gif    
/// assembly="Com.Daveranck.ExceptionHandler"  
InBlock.gif    
/// type="Com.Daveranck.ExceptionHandler.ExceptionLogPublisher" 
InBlock.gif    
/// fileName="AppExceptionLog.txt"
InBlock.gif    
/// filePath="c:"
InBlock.gif    
/// stackTrace = "true"
InBlock.gif    
/// daily="true" /&gt;
InBlock.gif    
///    </code>
InBlock.gif    
///    </example>
ExpandedSubBlockEnd.gif    
///    </remarks>

InBlock.gif
InBlock.gif    
public class ExceptionLogPublisher : IExceptionPublisher
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// defaults if no parameteres given
InBlock.gif
        private string m_LogName = @"ErrorLog.txt";
InBlock.gif        
private string m_LogPath = @"C:";
InBlock.gif        
private bool m_dailyLog = false;
InBlock.gif        
private bool m_stackTrace = false;
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Constructor
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ExceptionLogPublisher()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Sets up the config settings from the app
InBlock.gif        
/// config file, if available. Otherewise,
InBlock.gif        
/// defaults are used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void SetConfig(NameValueCollection configSettings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Load Config values if they are provided.
InBlock.gif
            if (configSettings != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (configSettings["fileName"!= null &&  
InBlock.gif                    configSettings[
"fileName"].Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{  
InBlock.gif                    m_LogName 
= configSettings["fileName"];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (configSettings["filePath"!= null &&  
InBlock.gif                    configSettings[
"filePath"].Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{  
InBlock.gif                    m_LogPath 
= configSettings["filePath"];
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(configSettings["daily"!= null &&
InBlock.gif                    configSettings[
"daily"].Equals("true"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    m_dailyLog 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(configSettings["stackTrace"!= null &&
InBlock.gif                    configSettings[
"stackTrace"].Equals("true"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    m_stackTrace 
= true;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Crestes the file path to write the log file to.
InBlock.gif        
/// If Daily has been set to true in config settings,
InBlock.gif        
/// will prepend the date (yyyymmdd) to the filename,
InBlock.gif        
/// allowing the file to rollover daily.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>string Filename and path</returns>

InBlock.gif        private string CreateLogFilePath()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(m_dailyLog == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Add date header to filename
InBlock.gif
                DateTime dt = DateTime.Today;
InBlock.gif                StringBuilder newLogName 
= new StringBuilder();
InBlock.gif                newLogName.Append(dt.Year.ToString());
InBlock.gif                newLogName.Append(dt.Month.ToString());
InBlock.gif                newLogName.Append(dt.Day.ToString());
InBlock.gif                newLogName.Append(m_LogName);
InBlock.gif                m_LogName 
= newLogName.ToString();
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string logFilePath = m_LogPath + m_LogName;
InBlock.gif            
return logFilePath;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates the message to be logged from the Exception.
InBlock.gif        
/// Will add additional info if it exists and if
InBlock.gif        
/// stackTrace is true in config settings, will append stack trace
InBlock.gif        
/// and source to the message.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>string message to log</returns>

InBlock.gif        private string CreateLogMessage(Exception exception, 
InBlock.gif            NameValueCollection additionalInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Create StringBuilder to maintain publishing information.
InBlock.gif
            StringBuilder strInfo = new StringBuilder();
InBlock.gif
InBlock.gif            
// Record the contents of the additionalInfo collection.
InBlock.gif
            if (additionalInfo != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Record General information.
InBlock.gif
                strInfo.AppendFormat("{0}General Information {0}", Environment.NewLine);
InBlock.gif                strInfo.AppendFormat(
"{0}Additonal Info:", Environment.NewLine);
InBlock.gif                
foreach (string i in additionalInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strInfo.AppendFormat(
"{0}{1}: {2}", Environment.NewLine, i, 
InBlock.gif                        additionalInfo.Get(i));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Append the exception text including:
InBlock.gif            
// Message and if stackTrace = true, Source and Stack Trace
InBlock.gif
            if ( exception != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strInfo.AppendFormat(
"{0}Exception Information {1}",
InBlock.gif                    Environment.NewLine, exception.Message.ToString() 
+ Environment.NewLine); 
InBlock.gif                
//Append Source and Stacktrace
InBlock.gif
                if(m_stackTrace == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strInfo.Append(Environment.NewLine 
+ "Source: ---- " + Environment.NewLine 
InBlock.gif                        
+ exception.Source + Environment.NewLine); 
InBlock.gif                    strInfo.Append(Environment.NewLine 
+ "Stack Trace: ---- " 
InBlock.gif                        
+ Environment.NewLine + exception.StackTrace);
ExpandedSubBlockEnd.gif                }

InBlock.gif                strInfo.Append(Environment.NewLine 
InBlock.gif                    
+ "------------------------------------------------------------" 
InBlock.gif                    
+ Environment.NewLine + Environment.NewLine);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strInfo.AppendFormat(
"{0}{0}No Exception.{0}", Environment.NewLine);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strInfo.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Provide implementation of the IExceptionPublisher interface
InBlock.gif        
/// This contains the single Publish method.
InBlock.gif        
/// <para>Logs the Exception Message, Source and Stack Trace</para>
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="exception">Exception</param>
InBlock.gif        
/// <param name="additionalInfo">NameValuecollection</param>
ExpandedSubBlockEnd.gif        
/// <param name="configSettings">NameValuecollection</param>

InBlock.gif        void IExceptionPublisher.Publish(Exception exception, 
InBlock.gif            NameValueCollection additionalInfo, 
InBlock.gif            NameValueCollection configSettings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Read and set up configuration
InBlock.gif
            SetConfig(configSettings);
InBlock.gif
InBlock.gif            
// Create fileName
InBlock.gif
            string logFilePath = CreateLogFilePath();
InBlock.gif
InBlock.gif            
// Create Message
InBlock.gif
            string logMessage = CreateLogMessage(exception, additionalInfo);
InBlock.gif
InBlock.gif            
// Write the entry to the log file. 
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Append to file if exists or create new file
InBlock.gif
                using ( FileStream fs = File.Open(logFilePath,
InBlock.gif                            FileMode.Append,FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
using (StreamWriter sw = new StreamWriter(fs))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sw.Write(logMessage);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Error writing exception.
InBlock.gif                
// Exception Manager will log original exception
InBlock.gif                
// and this exception to Event log
InBlock.gif
                throw ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

数据库异常的数据表如下:

None.gif if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[ErrorLog] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
None.gif
drop   table   [ dbo ] . [ ErrorLog ]
None.gif
GO
None.gif
None.gif
CREATE   TABLE   [ dbo ] . [ ErrorLog ]  (
None.gif    
[ ID ]   [ numeric ] ( 18 0 IDENTITY  ( 1 1 NOT   NULL  ,
None.gif    
[ Info ]   [ varchar ]  ( 500 NOT   NULL  ,
None.gif    
[ Message ]   [ varchar ]  ( 500 )   NULL  ,
None.gif    
[ Source ]   [ varchar ]  ( 255 )   NULL  ,
None.gif    
[ StackTrace ]   [ varchar ]  ( 1000 )   NULL  ,
None.gif    
[ EntryDateTime ]   [ datetime ]   NOT   NULL  
None.gif
ON   [ PRIMARY ]
None.gif
GO
None.gif
None.gif
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[spLogError] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
None.gif
drop   procedure   [ dbo ] . [ spLogError ]
None.gif
GO
None.gif
None.gif
SET  QUOTED_IDENTIFIER  OFF  
None.gif
GO
None.gif
SET  ANSI_NULLS  OFF  
None.gif
GO
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*********************************************************************************************************************************************************************************************
InBlock.gif    Name:            spLogError
InBlock.gif    Author:            Dave Ranck
InBlock.gif    Date:            11/14/2003
InBlock.gif
InBlock.gif    Purpose:        This procedure logs exception info. 
InBlock.gif
InBlock.gif    Parameters:        @Info
InBlock.gif                @Message
InBlock.gif                @Source
InBlock.gif                @StackTrace
InBlock.gif        
ExpandedBlockEnd.gif/********************************************************************************************************************************************************************************************
*/

None.gif
*/
None.gif
CREATE   PROCEDURE  spLogError
None.gif@Info 
varchar ( 500 ),
None.gif@Message 
varchar ( 500 ),
None.gif@Source 
varchar ( 255 ),
None.gif@StackTrace 
varchar  ( 2000 )
None.gif
None.gif
None.gif
AS
None.gif
INSERT   INTO  ErrorLog(Info, Message, Source, StackTrace, EntryDateTime) 
None.gif
None.gif
VALUES (@Info, @Message, @Source, @StackTrace,  GetDate ())
None.gif
GO
None.gif
SET  QUOTED_IDENTIFIER  OFF  
None.gif
GO
None.gif
SET  ANSI_NULLS  ON  
None.gif
GO


设定相应的异常通过CONFIG文件来设置,我在接面有相应的例子.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值