标签:ica 4.0 tin 分类 fsharp write 手动 处理异常 policy
把合理的异常信息显示给相相应的用户。
因此主要的异常处理脉络也出现了。1.识别异常类型 2.决定处理策略 3.异常转化。第二点不是必须的,实际我们能够什么都不做。我们看一个最主要的样例
let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,
PostHandlingAction.ThrowNewException,
[|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])]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")再进行封装操作。
首先应用日志模块生成一个日志处理对象
#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)
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")通过Fsharp探索Enterprise Library Exception
标签:ica 4.0 tin 分类 fsharp write 手动 处理异常 policy
原文地址:http://www.cnblogs.com/cxchanpin/p/6797719.html