Skip to content

Configuration

thushxr edited this page Jun 27, 2026 · 1 revision

Configuration

Configure the logger once at startup with Log.Configure(...). It's safe to call again to reconfigure — each call builds a fresh options object and swaps it in atomically.

Log.Configure(o => o
    .UseStructured()
    .SetMinimumLevel(LogLevel.Debug)
    .AddTraceId());

If you never call Configure, the logger runs with the defaults below.

All options & defaults

Fluent method Property Default Effect
UseStructured(bool) Structured false (text) JSON output when true, human-readable text when false
SetMinimumLevel(LogLevel) MinimumLevel Information Entries below this level are dropped
UseUtc(bool) UseUtcTimestamp true UTC timestamps when true, local time when false
TimestampFormat yyyy-MM-dd HH:mm:ss.fff Format string for the timestamp
AddTimestamp(bool) IncludeTimestamp true Render the timestamp field
AddLevel(bool) IncludeLevel true Render the [LEVEL] field
AddColorIcon(bool) IncludeColorIcon true Render the color box icon
AddTraceId(bool) IncludeTraceId true Render the trace-scope id (only shown when one is set)
UseClassName(bool) IncludeClassName true Render the class field — Log<T> only
UseMethodName(bool) IncludeMethodName true Render the method field — Log<T> only

Every toggle defaults to on and applies to both text and JSON output. Pass false to turn a field off:

Log.Configure(o => o.AddColorIcon(false).AddTimestamp(false));
// [INFO] Application started

Output modes

Text (default)

Log.Configure(o => o.UseStructured(false));   // or just don't call UseStructured
Log.Information("Server listening on {port}", 8080);
2026-06-27 09:14:02.318 🟩 [INFO] Server listening on 8080

Field order in text mode: timestamp · icon · [LEVEL] · [TraceId: …] · [class.method] · message · (exception on the next line).

JSON

Log.Configure(o => o.UseStructured());
Log.Information("Server listening on {port}", 8080);
{"timestamp":"2026-06-27 09:14:02.318","level":"Information","icon":"🟩","message":"Server listening on 8080","port":8080}

Field order in JSON mode: timestamp · level · icon · traceId · class · method · message · (your template fields) · exception.

Changing the level at runtime

To change just the minimum level without a full reconfigure:

Log.MinimumLevel = LogLevel.Warning;   // get or set directly

This is handy for toggling verbosity on the fly (e.g. from an admin endpoint or env var).

Timestamps

  • UTC vs local: UseUtc() (default) uses DateTime.UtcNow; UseUtc(false) uses DateTime.Now. For server/cloud logging, UTC is strongly recommended.

  • Format: set TimestampFormat to any standard or custom .NET date format string. It's applied with CultureInfo.InvariantCulture for stable, locale-independent output.

    Log.Configure(o => { });               // defaults
    // To customize the format, set the property inside Configure:
    Log.Configure(o => o.UseStructured());
    // (TimestampFormat is a settable property on the options object)

A note on the source toggles

UseClassName / UseMethodName control fields that only the typed Log<T> produces. The non-generic Log never captures a class or method, so toggling these has no effect on it. See Log vs Log<T> for the full explanation.

Color icons

Level Icon
Trace
Debug 🟦
Information 🟩
Warning 🟨
Error 🟥
Critical 🟪

The icon is rendered to the left of the level in text mode, and as the icon field in JSON. Turn it off with AddColorIcon(false) if your sink doesn't render emoji well.

Clone this wiki locally