Class ExceptionLoggingHelper
Utility class for logging exceptions.
Namespace: TehGM.Wolfringo.Utilities
Assembly: Wolfringo.Core.dll
Syntax
public static class ExceptionLoggingHelper
Remarks
By default, if exception is caught, and exception is logged inside of the handling block, the log scope will be lost.
The way around this is to log the exception using `when` keyword.
This class contains methods that return true so they can be used with `when` syntax.
Usage sample:
try
{
// ...
}
catch (Exception ex) when (ex.LogAsError(_log, "Exception has occured")) { }
More information: https://andrewlock.net/how-to-include-scopes-when-logging-exceptions-in-asp-net-core/#exceptions-inside-scope-blocks-lose-the-scope
Methods
| Improve this Doc View SourceLogAsCritical(Exception, ILogger, String, Object[])
Logs exception with critical log level.
Declaration
public static bool LogAsCritical(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.
LogAsDebug(Exception, ILogger, String, Object[])
Logs exception with debug log level.
Declaration
public static bool LogAsDebug(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.
LogAsError(Exception, ILogger, String, Object[])
Logs exception with error log level.
Declaration
public static bool LogAsError(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.
LogAsInformation(Exception, ILogger, String, Object[])
Logs exception with information log level.
Declaration
public static bool LogAsInformation(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.
LogAsTrace(Exception, ILogger, String, Object[])
Logs exception with trace log level.
Declaration
public static bool LogAsTrace(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.
LogAsWarning(Exception, ILogger, String, Object[])
Logs exception with warning log level.
Declaration
public static bool LogAsWarning(this Exception exception, ILogger log, string message, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | Exception message to log. |
ILogger | log | Logger instance. |
String | message | Log message template. |
Object[] | args | Structured log message arguments. |
Returns
Type | Description |
---|---|
Boolean | Always returns true. |
Remarks
See ExceptionLoggingHelper for more information.
This method is null-logger-safe - if log
is null, message and exception won't be logged.