Exception怎么生成是一回事,怎么展示又是还有一回事了。

Exception Block主要关注的点在于Exception信息的展示。Exception不同于一般的log信息,是系统设计者未考虑的错误情况。当异常出现时,错误的情况,或者暴露一些比較敏感的系统信息。或者将一些不怎么友好的信息显示给一些不怎么友好的客户。这时一个计算机异常就引入了一个客户异常,一个终极异常。所以异常处理的目标就是截断异常,进而恢复系统。

把合理的异常信息显示给相相应的用户。

因此主要的异常处理脉络也出现了。1.识别异常类型 2.决定处理策略 3.异常转化。第二点不是必须的,实际我们能够什么都不做。我们看一个最主要的样例

  1. let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,
  2. PostHandlingAction.ThrowNewException,
  3. [|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])]

这个ExceptionPolicyEntry类的构造函数就包括了以上三个基本点。第一个參数是须要识别的异常类。实际的应用中异常特化的越具有特征性我们也就越可以识别此异常。只使用Exception带字符串对分类处理并没有什么优点。第二个枚举类型代表了处理策略。在处理完毕后再次抛出异常和忽略此异常都是比較经常使用的情况。最后是提供异常转化的详细方法,这里我们看到的是一个WrapHandler,类似于装饰者模式给原始的异常加一层壳。

详细的应用例如以下。

  1. let policies = [new ExceptionPolicyDefinition("Wrap Exception", exceptionShielding)]
  2.  
  3. let exceptionManager = new ExceptionManager(policies)
  4. ExceptionPolicy.SetExceptionManager(exceptionManager)
  5.  
  6. //most simple example
  7. exceptionManager.Process((fun () -> 1/0 ), "Wrap Exception")

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





我们做一个带日志的异常处理的样例。将原异常的信息进行替换后存入日志文件。

再进行封装操作。

首先应用日志模块生成一个日志处理对象

  1. #if COMPILED
  2. #else
  3. #r "[Xpath]/packages/EnterpriseLibrary.Data.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Data.dll"
  4. #r "[Xpath]/packages/EnterpriseLibrary.Common.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Common.dll"
  5. #r "[Xpath]/packages/EnterpriseLibrary.ExceptionHandling.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll"
  6. #r "[Xpath]/packages/EnterpriseLibrary.ExceptionHandling.Logging.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll"
  7. #r "[Xpath]/packages/EnterpriseLibrary.Logging.6.0.1304.0/lib/NET45/Microsoft.Practices.EnterpriseLibrary.Logging.dll"
  8. #r "System"
  9. #r "System.Data"
  10. #r "System.Configuration"
  11. #r "System.ServiceModel"
  12. #endif
  13.  
  14. open System
  15. open System.Configuration
  16. open Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
  17. open Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging
  18. open Microsoft.Practices.EnterpriseLibrary.Logging
  19. open Microsoft.Practices.EnterpriseLibrary.Logging.Formatters
  20. open Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners
  21. open System.Diagnostics
  22.  
  23. //make logger
  24. //format
  25. 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})}")
  26. //listener
  27. let flatFileTraceListener = new FlatFileTraceListener(@"c:\Temp\this.log", "------------------------------", "------------------------------",formatter)
  28. let eventlog = new EventLog("Application", ".", "Enterprise Libray Logging")
  29. let eventlogTraceListener = new FormattedEventLogTraceListener(eventlog)
  30. //configuration
  31. let config = new LoggingConfiguration()
  32. config.AddLogSource("General", SourceLevels.All, true, [|flatFileTraceListener :> TraceListener|]) |> ignore
  33. let logWriter = new LogWriter(config)

兴许的代码和之前的并无太大差别,加Policy条目。引用时注意类型字符串。

  1. let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,
  2. PostHandlingAction.ThrowNewException,
  3. [|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])
  4. ]
  5.  
  6. let replacingException = [new ExceptionPolicyEntry(typeof<Exception>,
  7. PostHandlingAction.ThrowNewException,
  8. [|new ReplaceHandler("Application Error. Please contact rigid", typeof<Exception>)|])
  9. ]
  10. let loggingAndReplacing = [new ExceptionPolicyEntry(typeof<Exception>,
  11. PostHandlingAction.NotifyRethrow,
  12. [|new LoggingExceptionHandler("General", 1000, TraceEventType.Error, "Rigid Service", 5, typeof<TextExceptionFormatter>, logWriter);
  13. new ReplaceHandler("Application Error. Please contact rigid", typeof<Exception>)|])
  14. ]
  15.  
  16. let policies = [new ExceptionPolicyDefinition("Wrap Exception", exceptionShielding);
  17. new ExceptionPolicyDefinition("Replace Exception", replacingException);
  18. new ExceptionPolicyDefinition("Log and Replace Exception", loggingAndReplacing)]
  19.  
  20. let exceptionManager = new ExceptionManager(policies)
  21. ExceptionPolicy.SetExceptionManager(exceptionManager)
  22.  
  23. //most simple example
  24. exceptionManager.Process((fun () -> 1/0 ), "Log and Replace Exception")

以上最基本概念,技术重点在异常怎样分类组织。与权限进行相应。

通过Fsharp探索Enterprise Library Exception的更多相关文章

  1. 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索

    这篇就三种拦截模式进行一下探索. 特性总结   类型 特点 其它 InterfaceInterceptor Innstance 仅单接口 类内部函数互相引用无法引起拦截行为 TransparentPr ...

  2. 通过fsharp 使用Enterprise Library Unity 2

    接着Depandency Injection继续. 最想做的还是用现成的程序模块对程序进行行为注入.只是不急,在此之前自己写一个接口对象观察一下IInterceptionBehavior接口的功效. ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

  4. Enterprise Library系列文章目录(转载)

    1. Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (初级) 2. Microsoft Enterprise Lib ...

  5. Enterprise Library 5.0 系列教程

    1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...

  6. Enterprise Library 4 数据访问应用程序块

    Enterprise Library 数据访问应用程序块简化了实现常规数据访问功能的开发任务.应用程序可以在各种场景中使用此应用程序块,例如为显示而读取数据.传递数据穿过应用程序层( applicat ...

  7. Enterprise Library 服务问题

    在使用Enterprise Library而没有注册服务的时候会出现这样的问题,"Editing Post "Failed to create instances of perfo ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...

随机推荐

  1. spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。

    spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...

  2. oracle查询时遇到的坑

    select * from manu_routecard t left join manu_routecardlist mr on t.routecard_id = mr.routecard_id l ...

  3. HTTP基础--cookie机制和session机制

    1.介绍cookie和session的区别,怎么获取与使用?(这个问题比较开放,可深可浅,现在将这里涉及的主要问题总计如下答案) 答: 一.cookie机制和session机制的区别 cookie机制 ...

  4. windows 系统下git 的安装

    在linux系统下,可以直接在命令窗口安装和使用git.但是,在windows系统下,想要达到同样的效果,可以安装git,使用git bash到达效果.具体安装步骤如下: 第一步:官网上下载git 网 ...

  5. Android应用开发EditText文本内容变化监听方法

    import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android. ...

  6. laravel的elixir和gulp用来对前端施工

    使用laravel elixer npm install --global gulp  ok 然后在安装好的laravel 下 npm install 以安装 laravel-elixir subli ...

  7. JS省份联级下拉框

    <script type="text/javascript"> var china=new Object();china['北京市']=new Array('北京市区' ...

  8. 百度之星初赛(A)——T2

    数据分割 小w来到百度之星的赛场上,准备开始实现一个程序自动分析系统. 这个程序接受一些形如x_i = x_jx​i​​=x​j​​ 或 x_i \neq x_jx​i​​≠x​j​​ 的相等/不等约 ...

  9. 编译gdb 报错 No module named gdb.frames

    将源码目录下的python模块拷贝到指定目录即可 cd /root/gdb-7.12/ cp -rp gdb/python/lib/gdb /usr/local/share/gdb/python/ 编 ...

  10. DNS 资源记录解释

    ;SOA授权的开始;;SOA或授权的开始记录用来表示区域的启动;每个区域必须只有一个SOA记录;从名字服务器,在不能和主服务器通信的情况下,将提供12小时DNS服务, 在指定的时间后停止为那个区域提供 ...