Table of Contents

Class LoggerBaseWrapper<T>

Namespace
Arc4u.Diagnostics
Assembly
Arc4u.Diagnostics.dll

Base implementation of ILoggerWrapper<T>. It enriches every log entry with the Arc4u properties (see LoggingConstants) and the properties supplied by IAddPropertiesToLog before forwarding it to the underlying logger.

public abstract class LoggerBaseWrapper<T> : ILoggerWrapper<T>, ILogger<T>, ILogger, ILoggerCallerMember

Type Parameters

T

The category type of the logger.

Inheritance
LoggerBaseWrapper<T>
Implements
Derived
Inherited Members
Extension Methods

Remarks

SetContext(string, string, Type?) (used by Technical(), Business(), Monitoring(), ...) returns a new logger dedicated to one entry: the properties added to it are written with its next entry only, and they are never shared with other callers of this instance.

Constructors

LoggerBaseWrapper(ILoggerFactory, IAddPropertiesToLog, IOptionsMonitor<ApplicationConfig>)

Initializes a new instance of the LoggerBaseWrapper<T> class; created by the IServiceProvider. When the application configuration has no logging name, the name of the entry assembly is used.

public LoggerBaseWrapper(ILoggerFactory loggerFactory, IAddPropertiesToLog addPropertiesToLog, IOptionsMonitor<ApplicationConfig> appConfig)

Parameters

loggerFactory ILoggerFactory

The factory used to create the underlying logger.

addPropertiesToLog IAddPropertiesToLog

The provider of the additional properties added to every log entry.

appConfig IOptionsMonitor<ApplicationConfig>

The application configuration, used to obtain the application name.

Properties

AdditionalFields

Gets the additional properties that are written with the next log entry of this logger.

public Dictionary<string, object?> AdditionalFields { get; }

Property Value

Dictionary<string, object>

Remarks

The properties are cleared once the entry is written.

IncludeStackTrace

Sets a value indicating whether the stack trace is added to the next log entry; reset once the entry is written.

public bool IncludeStackTrace { set; }

Property Value

bool

Methods

BeginScope<TState>(TState)

Create a disposable scope that can be used to push properties into the scope. This is not really intended to be used by the end user. As LoggerWrapper is a wrapper around ILogger, this method must be implemented.

public IDisposable? BeginScope<TState>(TState state) where TState : notnull

Parameters

state TState

The scope state.

Returns

IDisposable

A disposable that ends the scope, or null.

Type Parameters

TState

The type of the scope state.

CallerMemberName(string)

Sets the name of the calling member written in the MethodName property of the next entry.

public void CallerMemberName(string caller)

Parameters

caller string

The name of the calling member.

Dispose()

Marks the wrapper as disposed; any later attempt to log throws an ObjectDisposedException.

public void Dispose()

IsEnabled(LogLevel)

Checks if the given logLevel is enabled.

public bool IsEnabled(LogLevel logLevel)

Parameters

logLevel LogLevel

Level to be checked.

Returns

bool

true if enabled.

Log<TState>(LogLevel, EventId, TState, Exception?, Func<TState, Exception?, string>)

Writes a log entry in the Technical category. When state is a sequence of key/value pairs, the pairs are written as properties of this entry. The entry is enriched with the Arc4u properties and forwarded to the underlying logger; for an AggregateException the flattened inner exceptions are logged as well. The AdditionalFields, the stack trace request and the calling member are reset once the entry is written.

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)

Parameters

logLevel LogLevel

The severity of the entry.

eventId EventId

The event id, forwarded to the underlying logger.

state TState

The state to log.

exception Exception

The exception related to the entry, if any.

formatter Func<TState, Exception, string>

Creates the message from the state and the exception.

Type Parameters

TState

The type of the state.

Exceptions

ObjectDisposedException

The wrapper has been disposed.

SetContext(string, string, Type?)

Returns a logger that writes its entries with the category, the calling member and optionally the source context type given.

public ILoggerWrapper<T> SetContext(string category, string caller = "", Type? realType = null)

Parameters

category string

The message category, see MessageCategory.

caller string

The name of the calling member.

realType Type

The type to report as the source context instead of T, or null to keep T.

Returns

ILoggerWrapper<T>

The logger wrapper to use for the entry.

Remarks

The returned logger has its own additional fields, so a logger shared between callers (or threads) is never modified by an entry being prepared.