The last thing you want is to have your application to fail in production because of a failure of the logging feature. In order to prevent this from happening, PostSharp catches all exceptions that are thrown by the logging code and allows you to override the behavior.
Default exception handling policy
By default, when an exception occurs in the logging feature, the following happens:
A message is logged to the Meta role. By default, the Meta role uses the default LoggingBackend, so it is likely that the logging of the logging failure will fail. It is recommended to configure the Meta logging role separately by using the following code snippet:
LoggingServices.Roles[LoggingRoles.Meta].Backend = new MyBackend();
The whole LoggingBackend responsible for the failure is disabled, unless the reason of the failure is LoggingBackend is a defect in user code (typically an invalid use of the Logger API) and not in the logging component itself. This pattern is called a circuit breaker. You can enable the LoggingBackend again by setting the IsEnabled property to
true
.
Warning
When the logging exception happens during the logging of a user-code exception, the details of the logging exception are not available. The reason for this behavior is that user-code exceptions are logged from exception filters in order to preserve the call stack, and exception thrown in exception filters cannot be caught.
Customizing the exception handling logic
To override the default exception handling behavior:
Create a class that implements the ILoggingExceptionHandler interface.
Assign the ExceptionHandler property to an instance of your class.
The following code snippet implements an exception handler that throws an exception upon any failure. The Initialize
method registers the exception handler.
class ThrowLoggingExceptionHandler : ILoggingExceptionHandler
{
public void OnInternalException( LoggingExceptionInfo exceptionInfo )
{
throw new Exception("Internal logging exception.", exceptionInfo.Exception);
}
public void OnInvalidUserCode( ref CallerInfo callerInfo, LoggingTypeSource source, string message, params object[] args )
{
throw new InvalidOperationException( string.Format( message, args ) );
}
public static void Initialize()
{
LoggingServices.ExceptionHandler = new ThrowLoggingExceptionHandler();
}
}
See Also
Reference