Explore Enterprise Library exception through fsharp

Source: Internet
Author: User

How to generate an exception is one thing, and how to display it is another thing.
Exception block mainly focuses on the display of exception information. Exception is an error that is not considered by the system designer. When an exception occurs, the error may occur, some sensitive system information may be exposed, or some unfriendly information may be displayed to some unfriendly customers. In this case, a computer exception introduces a customer exception and an ultimate exception. Therefore, the goal of exception handling is to cut off the exception and restore the system. Display reasonable exception information to the corresponding user.
Therefore, the basic exception handling process also emerged. 1. Identify the exception type 2. Determine the handling policy 3. Identify the exception type. The second point is not mandatory. In reality, we can do nothing. Let's look at a basic example.
let exceptionShielding = [new ExceptionPolicyEntry(typeof<Exception>,                                                        PostHandlingAction.ThrowNewException,                                                        [|new WrapHandler("Application Error. Please contact rigid", typeof<Exception>)|])]

The constructor of the exceptionpolicyentry class contains the above three basic points. The first parameter is the exception class to be identified. The more characteristic the exception becomes in the actual application, the more we can identify the exception. Using only the exception string does not have any benefit for classification processing. The second Enumeration type represents the processing policy. It is common to throw an exception again after processing and ignore the exception. The last step is to provide a specific method for exception conversion. Here we see a wraphandler, which is similar to the modifier mode that adds a shell to the original exception.
The specific application is as follows.
let policies = [new ExceptionPolicyDefinition("Wrap Exception", exceptionShielding)]let exceptionManager = new ExceptionManager(policies)ExceptionPolicy.SetExceptionManager(exceptionManager)//most simple exampleexceptionManager.Process((fun () -> 1/0 ), "Wrap Exception")

When an exception is obtained, try catch block is not used, but is implicitly processed through the process of exceptionmanager. Because all the processing operations are determined in advance, nothing is missing. There is no flexible way to handle exceptions here, or you can manually obtain the exception object for targeted processing.


We will provide an example of exception handling with logs, replace the original exception information and store it in the log file. Then perform encapsulation.
First, the application log module generates a log processing object.
#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"#endifopen Systemopen System.Configurationopen Microsoft.Practices.EnterpriseLibrary.ExceptionHandlingopen Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Loggingopen Microsoft.Practices.EnterpriseLibrary.Loggingopen Microsoft.Practices.EnterpriseLibrary.Logging.Formattersopen Microsoft.Practices.EnterpriseLibrary.Logging.TraceListenersopen System.Diagnostics//make logger//formatlet 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})}")//listenerlet flatFileTraceListener = new FlatFileTraceListener(@"c:\Temp\this.log", "------------------------------", "------------------------------",formatter)let eventlog = new EventLog("Application", ".", "Enterprise Libray Logging")let eventlogTraceListener = new FormattedEventLogTraceListener(eventlog)//configurationlet config = new LoggingConfiguration()config.AddLogSource("General", SourceLevels.All, true, [|flatFileTraceListener :> TraceListener|]) |> ignorelet logWriter = new LogWriter(config)

There is no major difference between the subsequent code and the previous Code. Add a policy entry and pay attention to the type string when referencing it.
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 exampleexceptionManager.Process((fun () -> 1/0 ), "Log and Replace Exception")

The above basic concepts focus on how to classify and organize exceptions and correspond to permissions.

Explore Enterprise Library exception through fsharp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.