Table of Contents

Class LoggingHelper

Namespace
Ecng.Logging
Assembly
Ecng.Logging.dll

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

Func<LogMessage, bool>

OnlyWarning

The filter that only accepts messages of Warning type.

public static readonly Func<LogMessage, bool> OnlyWarning

Field Value

Func<LogMessage, bool>

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

receiver ILogReceiver

Logs receiver.

message string

Text message.

args object[]

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

receiver ILogReceiver

Logs receiver.

exception Exception

Error details.

AddErrorLog(ILogReceiver, Exception, string)

To record an error to the log.

public static void AddErrorLog(this ILogReceiver receiver, Exception exception, string format)

Parameters

receiver ILogReceiver

Logs receiver.

exception Exception

Error details.

format string

A format string.

AddErrorLog(ILogReceiver, Func<string>)

To record an error to the log.

public static void AddErrorLog(this ILogReceiver receiver, Func<string> getMessage)

Parameters

receiver ILogReceiver

Logs receiver.

getMessage Func<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

receiver ILogReceiver

Logs receiver.

message string

Text message.

args object[]

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

receiver ILogReceiver

Logs receiver.

getMessage Func<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

receiver ILogReceiver

Logs receiver.

message string

Text message.

args object[]

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

receiver ILogReceiver

Logs receiver.

level LogLevels

The level of the log message.

getMessage Func<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

receiver ILogReceiver

Logs receiver.

message string

Text message.

args object[]

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

receiver ILogReceiver

Logs receiver.

getMessage Func<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

receiver ILogReceiver

Logs receiver.

message string

Text message.

args object[]

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

action Action

The 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

action Func<IDictionary<T, Exception>>

The function to execute that returns a dictionary.

Type Parameters

T

The 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

action Func<T>

The function to execute.

Returns

T

The resulting value, or the default value of T if an error occurs.

Type Parameters

T

The 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

messages IEnumerable<LogMessage>

The collection of messages to filter.

filters ICollection<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

source ILogSource

The 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

error Exception

Error.

format string

A format string.

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

task Task

The task to observe.

observer Action<Exception>

An action to handle exceptions if the task faults.

other Action<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

task Task

The 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

task Task

The 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

task Task<T>

The task to observe.

observer Action<Exception>

An action to handle exceptions if the task faults.

other Action<Task<T>>

An optional action to execute if the task completes successfully.

Returns

Task

A new task representing the continuation.

Type Parameters

T

The 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

from IEnumerable

The source enumerable.

func Func<T1, T2>

The converter function.

Returns

T2[]

An array containing the converted elements.

Type Parameters

T1

The type of the elements in the source enumerable.

T2

The 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

listener ILogListener

The log listener.

message LogMessage

The log message to write.