Custom Exceptions using Microsoft Exception Management Application Block

在看翻译文章之前,大家好要先看一下类介绍和类中的的方法,属性,构造器的List:(当然大家也可以看帮助,在这我帮大家列出基类的简介),只是小小的框架,并不是很复杂的,而且比较老,所以不要太深入啊,所以就没有发到园子,只是自己看这篇文章感觉可以学到东西.
List1:

1BaseApplicationException class
Constructor:
public BaseApplicationException();
public BaseApplicationException(string message);
public BaseApplicationException(string message, Exception inner);
public BaseApplicationException(SerializationInfo info, StreamingContext context);
Properties:
public string MachineName {get;}
public DateTime CreatedDateTime{get;}
public DateTime AppDomainName{get;}
public DateTime ThreadIdentityName{get;}
public DateTime WindowsIdentityName{get;}
public NameValueCollection AdditionalInformation{get;}

Methods:
public override void GetObjectData(SerializationInfo info,
StreamingContext context);

2:IExceptionPublisher

void Publish(Exception exception,

             NameValueCollection additionalInfo,

             NameValueCollection configSettings);

3:IExceptionXmlPublisher
void Publish(XmlDocument exceptionInfo, NameValueCollection configSettings);
NameValueCollection:是来于应用程序的异常管理设置。

List 2: classes And Interface

BaseApplicationException :这是异常的Base类,提供一个确保所有异常都能提供最小级别的信息提示,你能得到你自己定制的异常类的信息,

ExceptionManagerSectionHandler:用来重新得到你的应用程序中的Configuration文件的异常管理设置.

ExceptionManager:继承类通过你的应用程序与Exception Management Application block发布和日志异常相互结合.

ExceptionManagerInstallar:当你安装Exception Management Application
Block 后当被写入Microsoft Windows Event Log被默认发布者和定制发布者创建两个事件消息来源并被使用.

DefaultPublisher:提供基本日志功能和异常信息写入到时间日志.

IExceptionPublisher 和 IExceptionXmlPublisher.

(开始翻译)
Custom Exceptions using Microsoft Exception Management Application Block

这是关于Microsoft Exception Management Application Block 提供一个小的framework为操作异常,她还提供接口继承定制异常到日志,错误记录在不同的数据源的,如
   1. Text File
   2. Alert to Pager
   3. MSMQ
   4. Data base
   5. Email Notification

在文本和XML格式都来源于这个两个接口IExceptionPublisher,IExceptionXmlPublisher.

所以要创建一个定制异常发布者,你必须创建一个类来实现IExceptionPublisher或者IExceptionXmlPublisher节口, 你要引用Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.dll assembly.

This Exception Management Application Block,写默认异常详细信息到Windows Event Log,假设如果要接受异常成XML格式,就继承IExceptionXmlPublisher,或其他情况就继承 IExceptionPublisher.每一个接口包含单一方法调用Publish,直到你接收异常信息,另外还要设置Configuration和定制属性.

高级

关于一个一行代码你就能很容易的将异常信息写入系统时间日志中,或者创建定制发布者来扩展或创建一个类来实现ExceptionXmlPublisher or IExceptionPublisher将日志异常信息发布到其他数据源.

Exception Management Application Block是一个简单的框架能横容易的在.net程序上使用,当我们使用这个时候,能帮我们缩减错误处理,能帮我们更有效的确定我们应用程序中的错误操作查找方法.并且还为我们应用程序提供更好更容易的调试.还有帮助提供有效一致的异常管理.它还从商业管理逻辑代码隔离异常管理代码和日志异常定制的数量最小.

Here is the Implementation of Custom Exceptions Publisher using Exception Publisher.

This will store the Exception Details in Text Format

using System;
using System.Text;
using System.IO;
using System.Web.Mail ;
using System.Collections.Specialized;
using Microsoft.ApplicationBlocks.ExceptionManagement ;
namespace CustomExceptionManagement
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class ExceptionPublisher :IExceptionPublisher
{
public ExceptionPublisher()
{
}
private string _LogFileName = @"C:"DCWeatherErrorLog.txt";
private string _OpMail = "";
void IExceptionPublisher.Publish(Exception exception, NameValueCollection AdditionalInfo,NameValueCollection ConfigSettings)
{
// Load Config values if they are provided.
if (ConfigSettings != null)
{
if (ConfigSettings["fileName"] != null && ConfigSettings["fileName"].Length > 0)
{
_LogFileName = ConfigSettings["fileName"];
}
if (ConfigSettings["operatorMail"] !=null && ConfigSettings["operatorMail"].Length >
0)
{
_OpMail = ConfigSettings["operatorMail"];
}
}
// Create StringBuilder to maintain publishing information.
StringBuilder errInfo = new StringBuilder();
// Record the contents of the AdditionalInfo collection.
if (AdditionalInfo != null)
{
errInfo.AppendFormat("{0}Exception From:", Environment.NewLine);
foreach (string i in AdditionalInfo)
{
errInfo.AppendFormat("{0}{1}: {2}", Environment.NewLine, i, AdditionalInfo.Geti));
}
}
// Append the exception text
errInfo.AppendFormat("{0}{0}Exception Information{0}{1}", Environment.NewLine, exception.ToString());
// Write to the log file.
using (StreamWriter sw = File.AppendText(_LogFileName))
{
sw.WriteLine(errInfo.ToString());
sw.WriteLine("----------------------------------");
}
// Email
if (_OpMail.Length > 0)
{
string subject = "Exception Notification";
string body = errInfo.ToString();
SmtpMail.Send("seenivasa.ramadurai@Verizon.com", _OpMail, subject, body);
}
}
}
}

OUTPUT

Exception From:ExceptionManager.MachineName: SEENRAMADURAI
ExceptionManager.TimeStamp: 8/21/2003 12:37:11 PM
ExceptionManager.FullName: Microsoft.ApplicationBlocks.ExceptionManagement,
Version=1.0.1242.22429,Culture=neutral, PublicKeyToken=null
ExceptionManager.AppDomainName: DCWeatherService.exe
ExceptionManager.ThreadIdentity:
ExceptionManager.WindowsIdentity: ITS"SRamadurai

Exception Information
System.Data.SqlClient.SqlException: Login failed for user 'dcuapp'.
at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection
SqlConnectionString options,Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at DCWeatherService.Form1.GetZipCodes()in
d:"work"severobjects"dcweatherservice"form1.cs:line 244
----------------------------------

Here is the Implementation of Custom Exceptions Publisher using ExceptionXmlPublisher

This will store the Exception Details in XML Format

using System;
using System.Collections.Specialized;
using System.IO;
using System.Web.Mail ;
using Microsoft.ApplicationBlocks.ExceptionManagement ;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Messaging;
namespace CustomExceptionManagement
{
/// <summary>
/// Summary description for ExceptionPublisherXML.
/// </summary>
public class ExceptionXMLPublisher:IExceptionXmlPublisher
{
private string _OpMail = "";
private string logFilePath=null;
private string msmqName=@"."ErrorLog";
public ExceptionXMLPublisher()
{
logFilePath=ConfigurationSettings.AppSettings["LogFilePath"];
}
private static string CurrentDateAndTimeInFileFormat()
{
string CurrDate = DateTime.Now.ToString();
CurrDate= CurrDate.Replace('/','-');
return CurrDate=CurrDate.Replace(':','.');
}
void IExceptionXmlPublisher.Publish(XmlDocument ExceptionInfo, NameValueCollection ConfigSettings)
{
string _LogFileName = @"C:"ErrorLog.xml";
if (ConfigSettings != null)
{
if (ConfigSettings["operatorMail"] !=null && ConfigSettings["operatorMail"].Length > 0)
{
_OpMail = ConfigSettings["operatorMail"];
}
if (ConfigSettings["fileName"] != null)
{
_LogFileName = ConfigSettings["fileName"];
_LogFileName=logFilePath+CurrentDateAndTimeInFileFormat()+_LogFileName;
}
}
//This code will check configfile Email options is exists to notify the Error
if (_OpMail.Length > 0)
{
string subject = "Exception Notification";
string body = ExceptionInfo.OuterXml.ToString();
SmtpMail.Send("seenivasa.ramadurai@Verizon.com", _OpMail, subject, body);
}

using (StreamWriter sw = File.AppendText(_LogFileName))
{
sw.WriteLine(ExceptionInfo.OuterXml.ToString());
}
//This Code will check the QName in Configfile if exists then it will log the Error In MSMQ
if (ConfigSettings["QName"] != null)
{
msmqName=ConfigSettings["QName"];
MessageQueue msmq= new MessageQueue(msmqName);
Message msg= new Message();
msg.Label= "DCWeather";
msg.Body =ExceptionInfo.OuterXml.ToString();
msmq.Send(msg);
}
}
}
}

XML Output

Calling the Custom Exception in Application Code

static void Main(string[] args)
{
try
{
int a=10;
int b=0;
int c=a/b;
}
catch ( Exception ex)
{
ExceptionManager.Publish(ex);
}
}

Configuring a Custom Publisher

之后我们创建一个custom publisher组件,(这里要添加引用Microsoft.ApplicationBlocks.ExceptionManagement.dll ,Microsoft.ApplicationBlocks.ExceptionManagementInterface.dll),我们需要追加一个异常管理,所以要使用程序的.NET XML configuration文件(添加一个段配置),如下.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionManagement" type=
"Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft
.ApplicationBlocks.ExceptionManagement" />
</configSections>
<exceptionManagement mode="on">
<publisher assembly="CustomExceptionManagement"
type="CustomExceptionManagement.ExceptionPublisher"
operatorMail="seenivasa.ramadurai@its.verizon.com" />
<publisher assembly="CustomExceptionManagement"
type="CustomExceptionManagement.ExceptionPublisher" fileName="c:"VZMoneyError.txt" />
<publisher assembly="CustomExceptionManagement" type="CustomExceptionManagement.ExceptionXMLPublisher" exceptionFormat="xml" fileName="c:"VZMoneyError.XML" />
<publisher assembly="CustomExceptionManagement" type="CustomExceptionManagement.ExceptionXMLPublisher" exceptionFormat="xml" operatorMail="Nageswara.Tripuramallu@its.verizon.com" />
<publisher assembly="CustomExceptionManagement" type="CustomExceptionManagement.ExceptionXMLPublisher" exceptionFormat="xml" MSMQName="."ErrorLogsQ" />
</exceptionManagement>
</configuration>

---------------------------------------worksguo(翻译)-有什么问题,请指正。。谢谢----------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值