Question

I'm relatively new to Microsoft Enterprise Library. I'm currently explore using Enterprise Library 5 to do logging on exception thrown by the system into a text file.

Question 1

I came across LogEntry in Microsoft.Practices.EnterpriseLibrary.Logging comes with ErrorMessages and Message.

I hope that you all able to explain on which circumstances should use either ErrorMessages or Message? I can't find any definition on those two variable at the moment.

If I caught an Exception, should I assign ex.Message into ErrorMessages or Message?


Question 2

I also found out that there are Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging. May I know is this a more proper way to log exception instead of using Microsoft.Practices.EnterpriseLibrary.Logging?

Was it helpful?

Solution

If your purpose is to handle/log exceptions then you should be using the Exception Handling Block (of which Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging is a part). The EH Block allows you to configure how different types of exceptions are handled and create categories of exceptions. Then, when you need to handle an exception, you simple need to call:

ExceptionPolicy.HandleException( ex, <NameOfPolicy> );

The library will take care of the rest, routing the output to the correct listeners.

In addition, the EH Block has built-in functionality for fall-back logging. Let's say you're trying to log to a database but the database cannot be reached. You can configure the EH Block with a fallback (out of the box it uses the Windows Eventing system) and the fact that the log couldn't be created and the original exception will be logged to the fallback.

OTHER TIPS

May be too late but if in-case it helps someone.

You shouldn't use Microsoft.Practices.EnterpriseLibrary.Logging directly to log errors. It should be used only for general information logging.

Use Microsoft.Practices.EnterpriseLibrary.ExceptionHandling and Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging for error handling and error logging and configure it that way so that it uses one of the logging policies to log the error. What will be written in your log is depends on how your formatter is configured and where it will be logged is depends on your listner.

For the first question, You mostly use Message, ErrorMessages is more of extra information.EntLib ErrorMessages

For the second question:

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

it sits on top of Microsoft.Practices.EnterpriseLibrary.Logging, it mostly formats the string to be log-ready, so using it to log exceptions is optional and depends on your logging strategy. For example, I use Microsoft.Practices.EnterpriseLibrary.Logging to log everything, only cause I prefer working with minimal external references and the differences between the two is not that much. LoggingExceptionHandler

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top