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
TThe category type of the logger.
- Inheritance
-
LoggerBaseWrapper<T>
- Implements
-
ILogger<T>
- 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
loggerFactoryILoggerFactoryThe factory used to create the underlying logger.
addPropertiesToLogIAddPropertiesToLogThe provider of the additional properties added to every log entry.
appConfigIOptionsMonitor<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
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
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
stateTStateThe scope state.
Returns
- IDisposable
A disposable that ends the scope, or null.
Type Parameters
TStateThe 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
callerstringThe 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
logLevelLogLevelLevel to be checked.
Returns
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
logLevelLogLevelThe severity of the entry.
eventIdEventIdThe event id, forwarded to the underlying logger.
stateTStateThe state to log.
exceptionExceptionThe exception related to the entry, if any.
formatterFunc<TState, Exception, string>Creates the message from the state and the exception.
Type Parameters
TStateThe 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
categorystringThe message category, see MessageCategory.
callerstringThe name of the calling member.
realTypeTypeThe type to report as the source context instead of
T, or null to keepT.
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.