Class ILoggerExtensions
- Namespace
- Existential
- Assembly
- Existential.Net.dll
Provides extension methods for the Microsoft.Extensions.Logging.ILogger interface, 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 LogsThenHandle and LogsThenRethrow. The LogsThenHandle methods should be used when an exception is to be logged and then handled, while the LogsThenRethrow 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 the System.Exception being logged. 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 ILoggerExtensions
- Inheritance
-
System.ObjectILoggerExtensions
- 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
LogsThenHandle(ILogger, Exception, Action<ILogger, Exception>, 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 LogsThenHandle(this ILogger inLogger, Exception inException, Action<ILogger, Exception> inMessageDefinition, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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 no action.
Returns
- System.Boolean
Returns true as the result for the "when" clause, so that the exception will be handled by the catch.
LogsThenHandle<T1>(ILogger, Exception, Action<ILogger, T1, Exception>, 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 LogsThenHandle<T1>(this ILogger inLogger, Exception inException, Action<ILogger, T1, Exception> inMessageDefinition, T1 inArgument, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenHandle<T1, T2>(ILogger, Exception, Action<ILogger, T1, T2, Exception>, 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 LogsThenHandle<T1, T2>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenHandle<T1, T2, T3>(ILogger, Exception, Action<ILogger, T1, T2, T3, Exception>, 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 LogsThenHandle<T1, T2, T3>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenHandle<T1, T2, T3, T4>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, Exception>, 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 LogsThenHandle<T1, T2, T3, T4>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenHandle<T1, T2, T3, T4, T5>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, T5, Exception>, 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 LogsThenHandle<T1, T2, T3, T4, T5>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, T5, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenHandle<T1, T2, T3, T4, T5, T6>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception>, 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 LogsThenHandle<T1, T2, T3, T4, T5, T6>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, T6 inArgument6, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow(ILogger, Exception, Action<ILogger, Exception>, 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 LogsThenRethrow(this ILogger inLogger, Exception inException, Action<ILogger, Exception> inMessageDefinition, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1>(ILogger, Exception, Action<ILogger, T1, Exception>, 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 LogsThenRethrow<T1>(this ILogger inLogger, Exception inException, Action<ILogger, T1, Exception> inMessageDefinition, T1 inArgument, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1, T2>(ILogger, Exception, Action<ILogger, T1, T2, Exception>, 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 LogsThenRethrow<T1, T2>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1, T2, T3>(ILogger, Exception, Action<ILogger, T1, T2, T3, Exception>, 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 LogsThenRethrow<T1, T2, T3>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1, T2, T3, T4>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, Exception>, 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 LogsThenRethrow<T1, T2, T3, T4>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1, T2, T3, T4, T5>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, T5, Exception>, 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 LogsThenRethrow<T1, T2, T3, T4, T5>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, T5, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.
LogsThenRethrow<T1, T2, T3, T4, T5, T6>(ILogger, Exception, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception>, 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 LogsThenRethrow<T1, T2, T3, T4, T5, T6>(this ILogger inLogger, Exception inException, Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> inMessageDefinition, T1 inArgument1, T2 inArgument2, T3 inArgument3, T4 inArgument4, T5 inArgument5, T6 inArgument6, TextWriter inFallbackWriter = null)
Parameters
inLogger
Microsoft.Extensions.Logging.ILoggerThe logger to be used.
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.
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.