skip to content

When a Serilog sink silently writes nothing, how does SelfLog help you find out why, and what kinds of problems does it report?

level: middleimportance: should knowfreq 30%

answer

  1. Serilog swallows its own failures
  2. SelfLog.Enable(TextWriter or Action<string>)
  3. enable before building the logger
  4. "Unable to find a method called ..."
  5. WriteTo.Fallible and WriteTo.FallbackChain

basics

~20 s

Serilog never lets a logging failure throw into the app; it reports it to SelfLog, which is off by default. Calling SelfLog.Enable(Console.Error) before the logger is built prints misnamed sinks, unopenable files and sink exceptions.

solid answer

~40 s

Serilog treats logging as non-critical: sinks added with `WriteTo` run inside an aggregate that catches their exceptions, configuration problems are skipped, and nothing reaches your code. Those failures are written to `SelfLog`, Serilog's internal diagnostic channel, which discards everything until you call `SelfLog.Enable(...)` with a `TextWriter` or an `Action<string>`. Enable it first thing in `Program.cs`, before `ReadFrom.Configuration` or `CreateLogger`, because many problems happen while the pipeline is being built. Typical messages: "Unable to find a method called Fiile" from Serilog.Settings.Configuration, "unable to open file sink for logs/..." from the file sink, and "Caught exception while emitting to sink" from the core. In Serilog 4, `WriteTo.Fallible` and `WriteTo.FallbackChain` let production code react to sink failures instead of just reading them.

go deeper

for a junior

Remember that Serilog hides its own errors and that SelfLog.Enable(Console.Error) shows them.

for a middle

Explain why logging failures are swallowed, why SelfLog must be enabled before configuration is read, and the common messages for a misnamed sink or unopenable file.

for a senior

Use WriteTo.FallbackChain or WriteTo.Fallible with a failure listener to react to sink outages, and know AuditTo propagates failures by design.

for a principal

Decide when a logging outage must surface as an alert or a failed request, as with audit trails, rather than be tolerated silently.

## Why logging failures are invisible Serilog's design rule is that logging must never take the application down. Several mechanisms enforce it: - Sinks added with `WriteTo` are wrapped in a **safe aggregate sink** that catches any exception a sink throws while emitting. - Enricher exceptions are caught in the same way. - `Serilog.Settings.Configuration` skips a `WriteTo` entry whose name matches no method instead of throwing. - The file sink, when it cannot open its file, swaps in a failed sink that drops events. - Batched sinks report failed batches to a failure listener and keep retrying. (The exception is `AuditTo`: audit sinks are not wrapped and let exceptions propagate, because an audit trail must not fail silently.) The price of this safety is that a broken sink looks exactly like a quiet application. **SelfLog** is where Serilog tells you what it swallowed. ## Turning it on `SelfLog` is a static class in the core package, disabled by default. ```csharp using Serilog.Debugging; SelfLog.Enable(Console.Error); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .CreateLogger(); ``` The API has three members you need: 1. `SelfLog.Enable(TextWriter output)` - writes each message and flushes. The writer should be thread-safe; for a file, wrap it with `TextWriter.Synchronized(...)`. 2. `SelfLog.Enable(Action<string> output)` - for anything else, such as `msg => Debug.WriteLine(msg)`. 3. `SelfLog.Disable()` - turns it off again. Each message is prefixed with a UTC timestamp. Call `Enable` **before** the configuration is read: messages about a misnamed sink or an unopenable file are produced while the pipeline is being built, and a writer attached afterwards misses them. ## What it reports In a call-centre app whose logs vanished, the usual culprits look like this: | Symptom | SelfLog message (abridged) | Source | |---|---|---| | sink name typo or missing package | "Unable to find a method called Fiile..." with candidate methods | Serilog.Settings.Configuration | | file sink silent | "unable to open file sink for logs/callcentre-.txt" plus the exception | Serilog.Sinks.File | | a sink throws on emit | "Caught exception while emitting to sink ..." | Serilog core | | bad level on reload | "The value ... is not a valid Serilog level." | Serilog.Settings.Configuration | | enricher throws | "Exception ... caught while enriching ..." | Serilog core | SelfLog is a debugging aid, not a production channel: it is unstructured text and covers Serilog's own failures only. ## Reacting to failures in Serilog 4 Serilog 4 adds a structured failure path. Sinks that support it report through `ILoggingFailureListener`; by default that listener is `SelfLog.FailureListener`, which writes to SelfLog. Two `WriteTo` methods use it: - `WriteTo.Fallible(configureSink, failureListener)` - routes the sink's failures to your own listener, for example to raise a health signal. - `WriteTo.FallbackChain(configureSink, configureFallback)` - if the first sink cannot record an event, the event goes to the fallback sink, such as a local file when a log server is down. Both rely on the target sink either throwing synchronously or implementing `ISetLoggingFailureListener`, as the core's own batching sink does. ## Common root causes once SelfLog speaks - **Package missing or not loaded** - the sink package is not referenced, or the app is single-file and the `Using` list is absent. - **Wrong argument names** - `Args` keys are matched to the sink method's parameter names, ignoring case. A misspelled required argument (such as `path`) leaves no matching method, and SelfLog lists the candidates; a misspelled optional argument is simply ignored and its default applies, with no message at all. - **Paths relative to the wrong folder** - a relative file path resolves against the process working directory, which for a service is often not the app folder. - **Permissions** - the service account cannot create the log directory. ## A diagnosis routine 1. Enable SelfLog to standard error at the top of `Program.cs`. 2. Start the app and read the first lines: configuration problems appear before any event is written. 3. Fix the cause - add the package or `Using` entry, correct the name or path, grant the directory permission. 4. Remove SelfLog or leave it pointed at standard error, where it costs nothing while Serilog is healthy.

  • Why does a writer attached with SelfLog.Enable after CreateLogger miss the most useful messages?
    Many failures happen while the pipeline is built: `ReadFrom.Configuration` resolves sink names and the file sink opens its file during configuration. Those messages are emitted once, at that moment, so a writer attached afterwards never sees them. Enable SelfLog before any configuration is read.
  • Why do AuditTo sinks behave differently from WriteTo sinks when they fail?
    `WriteTo` sinks are wrapped in a safe aggregate that catches and self-logs exceptions. `AuditTo` sinks are not: a failure propagates to the logging call, because an audit record must either be written or visibly fail.

  • A misnamed sink in appsettings.json throws at startup
  • SelfLog writes to the console by default
  • SelfLog can be enabled at any point and still shows configuration errors
  • AuditTo sinks swallow exceptions just like WriteTo sinks
  • SelfLog is a structured sink you can query like application logs

Assumes Serilog 4.4; ILoggingFailureListener, SelfLog.FailureListener, WriteTo.Fallible and WriteTo.FallbackChain are Serilog 4 APIs.