Using the Exception Handling Block in the Corporate Library

We have an ASP.NET application that uses an Application Exception Handling block to log our exception to the database (indirectly using a logging block). This all works great. However, since it uses an exception handling block to register data, every time we want to register, we will need to new'd create a System.Exception object. Since we are not throwing an exception, there is no performance issue. However, we need to create a new exception object every time we want to write something. Is this bad design?

0


source to share


3 answers


You can define the logging policy independently of the exception handling policy, they should not be used together.

Exception handling and logging are used together many times by bundling listeners. For example, you can catch a specific error in a catch block and reference your exception handling policy, which can also include logging actions via logCategory.

On the other hand, to only do logging of certain events / actions, simply provide a log trace listener that does not reference the exception handling policy. Outputting your log message using the Trace.Write statement.

In any case, if you create different policies, you can connect listeners when needed for more flexibility.

Update: In the logonConfiguration section, configure another trace listener. Then you need to add a listener to the categorySources section in your web.config file. Below shows how to log in two sources, event log and xml file.



<categorySources>       
<add switchValue="All" name="Database Events">
    <listeners>
        <add name="Formatted EventLog TraceListener"/>
        <add name="XML TraceListener"/>
    </listeners>
</add></categorySources>

      

In your exceptionHandling section, make sure you connect your Database category content to your policy: (some details omitted for readability)

<exceptionHandling>
<exceptionPolicies>
    <add name="Data Access Policy">
            <exceptionTypes>
                <add type="System.OverflowException, mscorlib, ... postHandlingAction="NotifyRethrow" name="OverflowException">
                    <exceptionHandlers>
                        <add logCategory="Database Events" eventId="100" ... severity="Critical" name="Logging Handler"/>
                        <add exceptionMessage="Overflow Exception caught." ... >
                    </exceptionHandlers>
                </add>
            </exceptionTypes>
    </add>  
</exceptionPolicies></exceptionHandling>

      

So the end result in this case is a chain of two listeners referenced by the same policy. Note that it is handled from the system.overflow exception type - just an example of how you can specify different handlers for different exceptions.

And of course, you can just add a log listener and call it from your code when you need to log an event / action, without relying on exception handling. Let me know if you would like more information.

+3


source


I wouldn't say that. Its an exception handling block, not a registration block. Perhaps you should use EL LAB instead?



By the way, I love and use ELMAH as an exception handling block. It is also not meant to be logged, just to handle exceptions and log them to check later. I think there is a way to raise the exception event without a new Exception object.

0


source


Since you are already using the Logging Application Block and EL (even if it is indirect), you should just be able to log into the database through it and not throw an exception solely for logging.

I love ELMAH for exception handling too. I use this for raw logs and log4net for all other protocols.

0


source







All Articles