Table of Contents

Class LogService

Namespace
SharpConsoleUI.Logging
Assembly
SharpConsoleUI.dll

Library-managed logging service with circular buffer storage. Thread-safe implementation suitable for multi-threaded console applications. Supports optional file logging via environment variable or code configuration.

public sealed class LogService : ILogService, IDisposable
Inheritance
LogService
Implements
Inherited Members

Constructors

LogService()

Creates a new LogService instance. Automatically enables file logging if SHARPCONSOLEUI_DEBUG_LOG environment variable is set. Minimum log level can be overridden via SHARPCONSOLEUI_DEBUG_LEVEL (Trace/Debug/Information/Warning/Error/Critical).

public LogService()

Properties

Count

Gets the current number of log entries in the buffer

public int Count { get; }

Property Value

int

IsEnabled

Gets or sets whether logging is enabled

public bool IsEnabled { get; set; }

Property Value

bool

IsFileLoggingEnabled

Gets whether file logging is currently enabled

public bool IsFileLoggingEnabled { get; }

Property Value

bool

MaxBufferSize

Gets or sets the maximum number of log entries to retain in the buffer. Default: 1000

public int MaxBufferSize { get; set; }

Property Value

int

MinimumLevel

Gets or sets the minimum log level. Messages below this level are ignored. Default: Warning

public LogLevel MinimumLevel { get; set; }

Property Value

LogLevel

Methods

ClearLogs()

Clears all log entries from the buffer

public void ClearLogs()

DisableFileLogging()

Disables file logging and closes the log file

public void DisableFileLogging()

Dispose()

Disposes resources used by the log service

public void Dispose()

EnableFileLogging(string, bool)

Enables file logging to the specified path. The file will be created if it doesn't exist. Parent directories will be created if needed.

public void EnableFileLogging(string filePath, bool append = true)

Parameters

filePath string

Path to the log file

append bool

If true, appends to existing file; if false, overwrites

GetAllLogs()

Gets all log entries currently in the buffer

public IReadOnlyList<LogEntry> GetAllLogs()

Returns

IReadOnlyList<LogEntry>

Read-only list of all buffered log entries

GetRecentLogs(int)

Gets the most recent log entries

public IReadOnlyList<LogEntry> GetRecentLogs(int count = 100)

Parameters

count int

Maximum number of entries to return

Returns

IReadOnlyList<LogEntry>

Read-only list of log entries, newest first

Log(LogLevel, string, string?)

Logs a message at the specified level

public void Log(LogLevel level, string message, string? category = null)

Parameters

level LogLevel

The log level

message string

The message to log

category string

Optional category for grouping

LogCritical(string, Exception?, string?)

Logs a critical error message with optional exception

public void LogCritical(string message, Exception? exception = null, string? category = null)

Parameters

message string
exception Exception
category string

LogDebug(string, string?)

Logs a debug message

public void LogDebug(string message, string? category = null)

Parameters

message string
category string

LogError(string, Exception?, string?)

Logs an error message with optional exception

public void LogError(string message, Exception? exception = null, string? category = null)

Parameters

message string
exception Exception
category string

LogInfo(string, string?)

Logs an informational message

public void LogInfo(string message, string? category = null)

Parameters

message string
category string

LogTrace(string, string?)

Logs a trace message (most verbose)

public void LogTrace(string message, string? category = null)

Parameters

message string
category string

LogWarning(string, string?)

Logs a warning message

public void LogWarning(string message, string? category = null)

Parameters

message string
category string

Events

LogAdded

Raised when a new log entry is added (after passing the minimum level filter)

public event EventHandler<LogEntry>? LogAdded

Event Type

EventHandler<LogEntry>

LogsCleared

Raised when the log buffer is cleared

public event EventHandler? LogsCleared

Event Type

EventHandler