Repository navigation
03. Logging
Logging goes through the static Log facade. The default pipeline is queued and asynchronous — your caller never blocks on a slow file backend.
using ZeroLevel;
using ZeroLevel.Services.Logging;
Log.AddConsoleLogger(); // simplest
Log.AddDelegateLogger(line => Trace.WriteLine(line)); // lambda adapter
Log.AddTextFileLogger("logs"); // rolling text file
// encrypted text file with options
Log.AddEncryptedFileLogger(new EncryptedFileLogOptions()
.SetKey("password")
.SetMaximumFileSizeInKb(2048)
.SetFolderPath("logs"));
// richer text-file options: archiving + auto-clean + encoding + size cap
Log.AddTextFileLogger(new TextFileLoggerOptions()
.EnableAutoArchiving()
.EnableAutoCleaning(TimeSpan.FromDays(15))
.SetEncoding(Encoding.ASCII)
.SetFolderPath("logs")
.SetMaximumFileSizeInKb(1024 * 30));
// custom ILogger
Log.AddLogger(new MyLogger());A custom logger is just:
public class MyLogger : ILogger
{
public void Write(LogLevel level, string message)
=> Console.WriteLine($"[{DateTime.UtcNow.Ticks} {level}] {message}");
public void Dispose() { }
}Log.Backlog(1000); // cap queued messages (older entries drop first)
Log.Dispose(); // flush, detach all loggers, disableA single call wires up loggers from configuration keys:
Log.CreateLoggingFromConfiguration(Configuration.DefaultSet);Keys that CreateLoggingFromConfiguration understands:
| Key | Meaning |
|---|---|
console (flag) |
Attach a console logger. |
log or path
|
Folder for text-file logger. |
log.backlog |
Max queued messages. |
log.archive |
Zip logs after rotation. |
log.sizeinkb |
Max single file size. |
log.cleanolderdays |
Lifetime of archived files. |
When the keys live in a dedicated [log] section the prefix is stripped: just backlog, archive, sizeinkb, cleanolderdays.
Log.Info("request {0} took {1} ms", requestId, elapsed);
Log.Warning("queue size is {0} — slow consumer?", q.Count);
try
{
DoWork();
Log.Debug("success");
}
catch (Exception ex)
{
Log.Error(ex, "work failed: {0}", ex.Message);
}Available methods on Log:
void Raw(string line, params object[] args);
void Info(string line, params object[] args);
void Warning(string line, params object[] args);
void Error(string line, params object[] args);
void Error(Exception ex, string line, params object[] args);
void Fatal(string line, params object[] args);
void Fatal(Exception ex, string line, params object[] args);
void Debug(string line, params object[] args);
void Verbose(string line, params object[] args);
void SystemInfo(string line, params object[] args);
void SystemWarning(string line, params object[] args);
void SystemError(string line, params object[] args);
void SystemError(Exception ex, string line, params object[] args);The System* variants exist specifically for framework-internal messages (network stack, schedulers) so they can be filtered out of user-facing logs.
Bit-flag enum ZeroLevel.Services.Logging.LogLevel. Combine with |:
-
Info,Warning,Error,Fatal,Debug,Verbose,Raw -
System— umbrella forSystemInfo | SystemWarning | SystemError -
FullDebug— everything.
Console and file loggers each take their own level mask; filter independently.
Application basics
Data & serialization
Networking
Text & search