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
IsEnabled
Gets or sets whether logging is enabled
public bool IsEnabled { get; set; }
Property Value
IsFileLoggingEnabled
Gets whether file logging is currently enabled
public bool IsFileLoggingEnabled { get; }
Property Value
MaxBufferSize
Gets or sets the maximum number of log entries to retain in the buffer. Default: 1000
public int MaxBufferSize { get; set; }
Property Value
MinimumLevel
Gets or sets the minimum log level. Messages below this level are ignored. Default: Warning
public LogLevel MinimumLevel { get; set; }
Property Value
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
filePathstringPath to the log file
appendboolIf 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
countintMaximum 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
levelLogLevelThe log level
messagestringThe message to log
categorystringOptional 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
LogDebug(string, string?)
Logs a debug message
public void LogDebug(string message, string? category = null)
Parameters
LogError(string, Exception?, string?)
Logs an error message with optional exception
public void LogError(string message, Exception? exception = null, string? category = null)
Parameters
LogInfo(string, string?)
Logs an informational message
public void LogInfo(string message, string? category = null)
Parameters
LogTrace(string, string?)
Logs a trace message (most verbose)
public void LogTrace(string message, string? category = null)
Parameters
LogWarning(string, string?)
Logs a warning message
public void LogWarning(string message, string? category = null)
Parameters
Events
LogAdded
Raised when a new log entry is added (after passing the minimum level filter)
public event EventHandler<LogEntry>? LogAdded
Event Type
LogsCleared
Raised when the log buffer is cleared
public event EventHandler? LogsCleared