Class ExceptionExtensions
- Namespace
- Existential
- Assembly
- Existential.Net.dll
Provides extension methods for the System.Exception class, to support semantic logging of exceptions from within exception filters. Logging from within the exception filter ensures that the stack information for the exception is captured correctly, at the point that the exception is raised. Depending on the method chosen, it also allows exceptions to be logged regardless of whether they are subsequently handled.
The methods are named ShouldBeHandledLog and ShouldBeUnhandledLog. The ShouldBeHandledLog methods should be used when an exception is to be logged and then handled, while the ShouldBeUnhandledLog methods should be used when an exception is to be logged but not handled, allowing it to propagate up the call stack.
The methods accept a defined logger message , with up to six strongly typed parameters, and an implementation of Microsoft.Extensions.Logging.ILogger. An optional System.IO.TextWriter implementation can be provided to handle writing the error message to somewhere else in case logging fails. If no such implementation is provided, and logging fails, the message will be written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
public static class ExceptionExtensions
- Inheritance
-
System.ObjectExceptionExtensions
- Inherited Members
-
System.Object.Equals(System.Object)System.Object.Equals(System.Object, System.Object)System.Object.GetHashCode()System.Object.GetType()System.Object.MemberwiseClone()System.Object.ReferenceEquals(System.Object, System.Object)System.Object.ToString()
Methods
ShouldBeHandledLog(Exception, Action<ILogger, Exception>, ILogger, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog(this Exception inException, Action<ILogger, Exception> inMessageDefinition, ILogger inLogger, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
ShouldBeHandledLog<T1>(Exception, Action<ILogger, T1, Exception>, ILogger, T1, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1>(this Exception inException, Action<ILogger, T1, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument
T1An argument of type
T1
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
ShouldBeHandledLog<T1, T2>(Exception, Action<ILogger, T1, T2, Exception>, ILogger, T1, T2, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1, T2>(this Exception inException, Action<ILogger, T1, T2, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
ShouldBeHandledLog<T1, T2, T3>(Exception, Action<ILogger, T1, T2, T3, Exception>, ILogger, T1, T2, T3, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1, T2, T3>(this Exception inException, Action<ILogger, T1, T2, T3, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
ShouldBeHandledLog<T1, T2, T3, T4>(Exception, Action<ILogger, T1, T2, T3, T4, Exception>, ILogger, T1, T2, T3, T4, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1, T2, T3, T4>(this Exception inException, Action<ILogger, T1, T2, T3, T4, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
ShouldBeHandledLog<T1, T2, T3, T4, T5>(Exception, Action<ILogger, T1, T2, T3, T4, T5, Exception>, ILogger, T1, T2, T3, T4, T5, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1, T2, T3, T4, T5>(this Exception inException, Action<ILogger, T1, T2, T3, T4, T5, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inArgument5
T5An argument of type
T5
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
T5
The type of the fifth argument passed to the log message.
ShouldBeHandledLog<T1, T2, T3, T4, T5, T6>(Exception, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception>, ILogger, T1, T2, T3, T4, T5, T6, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will then be handled by the catch block.
public static bool ShouldBeHandledLog<T1, T2, T3, T4, T5, T6>(this Exception inException, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, T6 inArgument6, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and handled.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, T6, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inArgument5
T5An argument of type
T5
to be passed to the log message.inArgument6
T6An argument of type
T6
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
T5
The type of the fifth argument passed to the log message.
T6
The type of the sixth argument passed to the log message.
ShouldBeUnhandledLog(Exception, Action<ILogger, Exception>, ILogger, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog(this Exception inException, Action<ILogger, Exception> inMessageDefinition, ILogger inLogger, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
ShouldBeUnhandledLog<T1>(Exception, Action<ILogger, T1, Exception>, ILogger, T1, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1>(this Exception inException, Action<ILogger, T1, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument
T1An argument of type
T1
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
ShouldBeUnhandledLog<T1, T2>(Exception, Action<ILogger, T1, T2, Exception>, ILogger, T1, T2, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1, T2>(this Exception inException, Action<ILogger, T1, T2, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
ShouldBeUnhandledLog<T1, T2, T3>(Exception, Action<ILogger, T1, T2, T3, Exception>, ILogger, T1, T2, T3, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1, T2, T3>(this Exception inException, Action<ILogger, T1, T2, T3, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
ShouldBeUnhandledLog<T1, T2, T3, T4>(Exception, Action<ILogger, T1, T2, T3, T4, Exception>, ILogger, T1, T2, T3, T4, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1, T2, T3, T4>(this Exception inException, Action<ILogger, T1, T2, T3, T4, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
ShouldBeUnhandledLog<T1, T2, T3, T4, T5>(Exception, Action<ILogger, T1, T2, T3, T4, T5, Exception>, ILogger, T1, T2, T3, T4, T5, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1, T2, T3, T4, T5>(this Exception inException, Action<ILogger, T1, T2, T3, T4, T5, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inArgument5
T5An argument of type
T5
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
T5
The type of the fifth argument passed to the log message.
ShouldBeUnhandledLog<T1, T2, T3, T4, T5, T6>(Exception, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception>, ILogger, T1, T2, T3, T4, T5, T6, TextWriter)
For use in the "when" clause of a try/catch block; creates a semantic log entry for an exception that will be otherwise left unhandled.
public static bool ShouldBeUnhandledLog<T1, T2, T3, T4, T5, T6>(this Exception inException, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> inMessageDefinition, ILogger inLogger, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, T6 inArgument6, TextWriter inFallbackWriter = null)
Parameters
inException
System.ExceptionThe exception to be logged and rethrown.
inMessageDefinition
System.Action<Microsoft.Extensions.Logging.ILogger, T1, T2, T3, T4, T5, T6, System.Exception>A log message definition created using the most appropriate overload of the LoggerMessage.Define method.
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
inArgument1
T1An argument of type
T1
to be passed to the log message.inArgument2
T2An argument of type
T2
to be passed to the log message.inArgument3
T3An argument of type
T3
to be passed to the log message.inArgument4
T4An argument of type
T4
to be passed to the log message.inArgument5
T5An argument of type
T5
to be passed to the log message.inArgument6
T6An argument of type
T6
to be passed to the log message.inFallbackWriter
System.IO.TextWriterAn optional System.IO.TextWriter that can report the exception if the logging action fails. Null by default, which will result in the message being written as an error to the trace listeners registered in the System.Diagnostics.Trace.Listeners collection.
Returns
- System.Boolean
Returns false as the result for the "when" clause, so that the exception is not handled in the catch and can be re-thrown.
Type Parameters
T1
The type of the first argument passed to the log message.
T2
The type of the second argument passed to the log message.
T3
The type of the third argument passed to the log message.
T4
The type of the fourth argument passed to the log message.
T5
The type of the fifth argument passed to the log message.
T6
The type of the sixth argument passed to the log message.