07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

252<br />

<strong>Enterprise</strong> <strong>Library</strong> <strong>Test</strong> <strong>Guide</strong><br />

Verification<br />

Running the test case again after applying the preceding code verified that the application<br />

block could retrieve configuration information from the dictionary configuration<br />

source.<br />

<strong>Test</strong>ing the Exception Handling Application Block<br />

The test case determined whether the Exception Handling Application Block logged<br />

the information retrieved by the .NET Framework Exception.Data property. This<br />

property gets a collection of key/value pairs that provide additional, user-defined<br />

information about an exception.<br />

<strong>Test</strong> Case<br />

The test case created an Exception object and added additional information to the<br />

Data collection. The test case used the ExceptionPolicy.HandleException method<br />

and checked to see whether the additional information, in addition to the exception,<br />

was logged.<br />

Problem<br />

The test case failed because the additional information was not logged. The LoggingExceptionHandler.HandlerException<br />

method passes the exception message and<br />

the stack trace but not the additional information that is stored in the Data collection.<br />

Therefore, this information is not logged. The following is the code that caused the<br />

failure.<br />

public Exception HandleException(Exception exception, Guid handlingInstanceId)<br />

{<br />

WriteToLog(CreateMessage(exception, handlingInstanceId));<br />

return exception;<br />

}<br />

Solution<br />

The WriteToLog method was modified. A new parameter named exception.Data was<br />

added to the method signature. This parameter passes the additional information.<br />

Also, within the WriteToLog method, code was added that enumerated the Data<br />

collection and added that information to the ExtendedProperties collection of the<br />

LogEntry instance. The following is the modified code.<br />

public Exception HandleException(Exception exception, Guid handlingInstanceId)<br />

{<br />

WriteToLog(CreateMessage(exception, handlingInstanceId), exception.Data);<br />

return exception;<br />

}<br />

protected virtual void WriteToLog(string logMessage, IDictionary exceptionData)<br />

{<br />

…<br />

foreach (DictionaryEntry dataEntry in exceptionData)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!