Class LoggingHelper
Extension class for ILogSource.
public static class LoggingHelper
- Inheritance
-
LoggingHelper
- Inherited Members
Fields
OnlyError
The filter that only accepts messages of Error type.
public static readonly Func<LogMessage, bool> OnlyError
Field Value
OnlyWarning
The filter that only accepts messages of Warning type.
public static readonly Func<LogMessage, bool> OnlyWarning
Field Value
Methods
AddDebugLog(ILogReceiver, string, params object[])
To record a debug message to the log.
public static void AddDebugLog(this ILogReceiver receiver, string message, params object[] args)
Parameters
receiverILogReceiverLogs receiver.
messagestringText message.
argsobject[]Text message settings. Used if the message is a format string.
AddErrorLog(ILogReceiver, Exception)
To record an error to the log.
public static void AddErrorLog(this ILogReceiver receiver, Exception exception)
Parameters
receiverILogReceiverLogs receiver.
exceptionExceptionError details.
AddErrorLog(ILogReceiver, Exception, string)
To record an error to the log.
public static void AddErrorLog(this ILogReceiver receiver, Exception exception, string format)
Parameters
receiverILogReceiverLogs receiver.
exceptionExceptionError details.
formatstringA format string.
AddErrorLog(ILogReceiver, Func<string>)
To record an error to the log.
public static void AddErrorLog(this ILogReceiver receiver, Func<string> getMessage)
Parameters
receiverILogReceiverLogs receiver.
getMessageFunc<string>The function returns the text for Message.
AddErrorLog(ILogReceiver, string, params object[])
To record an error to the log.
public static void AddErrorLog(this ILogReceiver receiver, string message, params object[] args)
Parameters
receiverILogReceiverLogs receiver.
messagestringText message.
argsobject[]Text message settings. Used if the message is a format string.
AddInfoLog(ILogReceiver, Func<string>)
To record a message to the log.
public static void AddInfoLog(this ILogReceiver receiver, Func<string> getMessage)
Parameters
receiverILogReceiverLogs receiver.
getMessageFunc<string>The function returns the text for Message.
AddInfoLog(ILogReceiver, string, params object[])
To record a message to the log.
public static void AddInfoLog(this ILogReceiver receiver, string message, params object[] args)
Parameters
receiverILogReceiverLogs receiver.
messagestringText message.
argsobject[]Text message settings. Used if the message is a format string.
AddLog(ILogReceiver, LogLevels, Func<string>)
To record a message to the log.
public static void AddLog(this ILogReceiver receiver, LogLevels level, Func<string> getMessage)
Parameters
receiverILogReceiverLogs receiver.
levelLogLevelsThe level of the log message.
getMessageFunc<string>The function that returns the text for Message.
AddVerboseLog(ILogReceiver, string, params object[])
To record a verbose message to the log.
public static void AddVerboseLog(this ILogReceiver receiver, string message, params object[] args)
Parameters
receiverILogReceiverLogs receiver.
messagestringText message.
argsobject[]Text message settings. Used if the message is a format string.
AddWarningLog(ILogReceiver, Func<string>)
To record a warning to the log.
public static void AddWarningLog(this ILogReceiver receiver, Func<string> getMessage)
Parameters
receiverILogReceiverLogs receiver.
getMessageFunc<string>The function returns the text for Message.
AddWarningLog(ILogReceiver, string, params object[])
To record a warning to the log.
public static void AddWarningLog(this ILogReceiver receiver, string message, params object[] args)
Parameters
receiverILogReceiverLogs receiver.
messagestringText message.
argsobject[]Text message settings. Used if the message is a format string.
DoWithLog(Action)
Wrap the specified action in a try/catch clause with logging.
public static void DoWithLog(this Action action)
Parameters
actionActionThe action to execute.
DoWithLog<T>(Func<IDictionary<T, Exception>>)
Executes the function that returns a dictionary, logs any exceptions, and logs a specific error for each key/value pair.
public static void DoWithLog<T>(Func<IDictionary<T, Exception>> action)
Parameters
actionFunc<IDictionary<T, Exception>>The function to execute that returns a dictionary.
Type Parameters
TThe type of the dictionary key.
DoWithLog<T>(Func<T>)
Wrap the specified function in a try/catch clause with logging.
public static T DoWithLog<T>(this Func<T> action)
Parameters
actionFunc<T>The function to execute.
Returns
- T
The resulting value, or the default value of T if an error occurs.
Type Parameters
TThe type of the returned result.
Filter(IEnumerable<LogMessage>, ICollection<Func<LogMessage, bool>>)
Filters messages based on provided filters.
public static IEnumerable<LogMessage> Filter(this IEnumerable<LogMessage> messages, ICollection<Func<LogMessage, bool>> filters)
Parameters
messagesIEnumerable<LogMessage>The collection of messages to filter.
filtersICollection<Func<LogMessage, bool>>A collection of filter predicates to determine which messages to include.
Returns
- IEnumerable<LogMessage>
An enumerable of filtered messages.
GetLogLevel(ILogSource)
Get LogLevel for the source. If the value is equal to Inherit, then the parental source level is taken.
public static LogLevels GetLogLevel(this ILogSource source)
Parameters
sourceILogSourceThe log source.
Returns
- LogLevels
The logging level.
LogError(Exception, string)
To record an error to the Application.
public static void LogError(this Exception error, string format = null)
Parameters
ObserveError(Task, Action<Exception>, Action<Task>)
Continues the task, observing any errors and optionally executing the specified action.
public static Task ObserveError(this Task task, Action<Exception> observer, Action<Task> other = null)
Parameters
taskTaskThe task to observe.
observerAction<Exception>An action to handle exceptions if the task faults.
otherAction<Task>An optional action to execute if the task completes successfully.
Returns
- Task
A new task representing the continuation.
ObserveErrorAndLog(Task)
Observes errors from the task and logs them.
public static Task ObserveErrorAndLog(this Task task)
Parameters
taskTaskThe task to observe.
Returns
- Task
A new task representing the continuation.
ObserveErrorAndTrace(Task)
Observes errors from the task and traces them using WriteLine(object).
public static Task ObserveErrorAndTrace(this Task task)
Parameters
taskTaskThe task to observe.
Returns
- Task
A new task representing the continuation.
ObserveError<T>(Task<T>, Action<Exception>, Action<Task<T>>)
Continues the generic task, observing any errors and optionally executing the specified action.
public static Task ObserveError<T>(this Task<T> task, Action<Exception> observer, Action<Task<T>> other = null)
Parameters
taskTask<T>The task to observe.
observerAction<Exception>An action to handle exceptions if the task faults.
otherAction<Task<T>>An optional action to execute if the task completes successfully.
Returns
- Task
A new task representing the continuation.
Type Parameters
TThe type of the task result.
SafeAdd<T1, T2>(IEnumerable, Func<T1, T2>)
Safely converts each element of an enumerable collection using a specified converter function. Logs any exceptions that occur during conversion.
public static T2[] SafeAdd<T1, T2>(this IEnumerable from, Func<T1, T2> func)
Parameters
fromIEnumerableThe source enumerable.
funcFunc<T1, T2>The converter function.
Returns
- T2[]
An array containing the converted elements.
Type Parameters
T1The type of the elements in the source enumerable.
T2The type of the elements in the resulting array.
WriteMessage(ILogListener, LogMessage)
Writes a single log message using the specified listener.
public static void WriteMessage(this ILogListener listener, LogMessage message)
Parameters
listenerILogListenerThe log listener.
messageLogMessageThe log message to write.