通过Fsharp探索Enterprise Library Exception

Exception怎么生成是一回事,怎么展示又是另一回事了。
Exception Block主要关注的点在于Exception信息的展示。Exception不同于一般的log信息,是系统设计者未考虑的错误情况。当异常出现时,错误的情况,或者暴露一些比较敏感的系统信息,或者将一些不怎么友好的信息显示给一些不怎么友好的客户。这时一个计算机异常就引入了一个客户异常,一个终极异常。所以异常处理的目标就是截断异常,进而恢复系统。把合理的异常信息显示给相对应的用户。
因此基本的异常处理脉络也出现了。1.识别异常类型 2.决定处理策略 3.异常转化。第二点不是必须的,实际我们可以什么都不做。我们看一个最基本的例子
let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,
                                                        PostHandlingAction.ThrowNewException,
                                                        [|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])]

这个ExceptionPolicyEntry类的构造函数就包含了以上三个基本点。第一个参数是需要识别的异常类,实际的应用中异常特化的越具有特征性我们也就越能够识别此异常。仅仅使用Exception带字符串对分类处理并没有什么好处。第二个枚举类型代表了处理策略。在处理完成后再次抛出异常和忽略此异常都是比较常用的情况。最后是提供异常转化的具体方法,这里我们看到的是一个WrapHandler,类似于装饰者模式给原始的异常加一层壳。
具体的应用如下。
let policies = [new ExceptionPolicyDefinition("Wrap Exception", exceptionShielding)]

let exceptionManager = new ExceptionManager(policies)
ExceptionPolicy.SetExceptionManager(exceptionManager)

//most simple example
exceptionManager.Process((fun () -> 1/0 ), "Wrap Exception")

获取异常时我们不再使用try catch块,而是通过ExceptionManager的Process进行隐式处理。因为所有该进行的处理都在事先确定了,所以并不缺少什么。这里也并非没有灵活处理异常的手段,也可以手动获得异常对象有针对性的进行处理。


我们做一个带日志的异常处理的例子,将原异常的信息进行替换后存入日志文件。再进行封装操作。
首先应用日志模块生成一个日志处理对象
#if COMPILED
#else
#r "[Xpath]/packages/EnterpriseLibrary.Data.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Data.dll"
#r "[Xpath]/packages/EnterpriseLibrary.Common.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Common.dll"
#r "[Xpath]/packages/EnterpriseLibrary.ExceptionHandling.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll"
#r "[Xpath]/packages/EnterpriseLibrary.ExceptionHandling.Logging.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll"
#r "[Xpath]/packages/EnterpriseLibrary.Logging.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Logging.dll"
#r "System"
#r "System.Data"
#r "System.Configuration"
#r "System.ServiceModel"
#endif

open System
open System.Configuration
open Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
open Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging
open Microsoft.Practices.EnterpriseLibrary.Logging
open Microsoft.Practices.EnterpriseLibrary.Logging.Formatters
open Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners
open System.Diagnostics

//make logger
//format
let formatter = new TextFormatter("TimeStamp: {timestamp}{newline}Message:{message}{newline}Category:{category}{newline}Priority:{priority}{newline}EventID:{eventid}{newline}Severity:{severity}{newline}Title:{title}{newline}Machine:{machine}{newline}App Domain:{localAppDomain}{newline}ProcessID:{localProcessId}{newline}Process Name:{localProcessName}{newline}Thread Name:{threadName}{newline}Win32 ThreadID:{win32Thread}{newline}Extended Properties:{dictinary({key}-{value}{newline})}")
//listener
let flatFileTraceListener = new FlatFileTraceListener(@"c:\Temp\this.log", "------------------------------", "------------------------------",formatter)
let eventlog = new EventLog("Application", ".", "Enterprise Libray Logging")
let eventlogTraceListener = new FormattedEventLogTraceListener(eventlog)
//configuration
let config = new LoggingConfiguration()
config.AddLogSource("General", SourceLevels.All, true, [|flatFileTraceListener :> TraceListener|]) |> ignore
let logWriter = new LogWriter(config)

后续的代码和之前的并无太大区别,加Policy条目,引用时注意类型字符串。
let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,
                                                        PostHandlingAction.ThrowNewException,
                                                        [|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])
                         ]

let replacingException = [new ExceptionPolicyEntry(typeof<Exception>,
                                                        PostHandlingAction.ThrowNewException,
                                                        [|new ReplaceHandler("Application Error. Please contact rigid", typeof<Exception>)|])
                         ]
let loggingAndReplacing = [new ExceptionPolicyEntry(typeof<Exception>,
                                                        PostHandlingAction.NotifyRethrow,
                                                        [|new LoggingExceptionHandler("General", 1000, TraceEventType.Error, "Rigid Service", 5, typeof<TextExceptionFormatter>, logWriter);
                                                          new ReplaceHandler("Application Error. Please contact rigid", typeof<Exception>)|])
                         ]

let policies = [new ExceptionPolicyDefinition("Wrap Exception", exceptionShielding);
                new ExceptionPolicyDefinition("Replace Exception", replacingException);
                new ExceptionPolicyDefinition("Log and Replace Exception", loggingAndReplacing)]

let exceptionManager = new ExceptionManager(policies)
ExceptionPolicy.SetExceptionManager(exceptionManager)

//most simple example
exceptionManager.Process((fun () -> 1/0 ), "Log and Replace Exception")

以上最基本概念,技术重点在异常如何分类组织,与权限进行对应。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值