CachedLogger

Logger that caches captured log messages in memory and periodically dumps them.

Inherits: Logger

Description

The CachedLogger class allows you to create a logger that caches captured log messages in memory and periodically dumps them.

Important points

  • Child classes implement saving cached messages to their specified destinations.

Configuration parameters

  • level: maximum log level to capture
  • source: source’s (context) name
  • options:
    • interval: interval in milliseconds to save log messages (default: 10 seconds).
    • max_cache_size: maximum number of messages stored in this cache (default: 100).

References

  • *:context-info:*:*:1.0 - (optional) ContextInfo to detect the context id and specify the counters' source

Fields

_cache

List containing log messages.

protected _cache: List<LogMessage>

_updated

Boolean variable indicating whether there has been an update or not.

protected _updated: bool = false

_lastDumpTime

Time of the last dump

protected _lastDumpTime: long = DateTime.UtcNow.Ticks

_maxCacheSize

Maximum number of messages stored in the cache (default: 100)

protected _maxCacheSize: int = 100

_interval

Interval in milliseconds to save log messages (default: 10 seconds)

protected _interval: int = 10000

_lock

Lock

protected _lock: object

Instance methods

Clear

Clears (removes) all cached log messages.

void Clear()

Configure

Configures a component by passing its configuration parameters.

public override void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

Dump

Dumps (writes) the currently cached log messages.

public void Dump()

Update

Sets message cache as updated and dumps it when timeout expires.

protected void Update()

Write

Writes a log message to the logger’s destination.

public override void Write(LogLevel level, string correlationId, Exception error, string message)

  • level: LogLevel - log level.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • error: Exception - error object associated with this message.
  • message: string - human-readable message to log.

Abstract methods

Save

Saves log messages from the cache.

protected abstract Save(List<LogMessage> messages)

See also