Skip to content

Application settings

Mykhailo Shevchuk edited this page Jun 6, 2026 · 17 revisions

The sink integrates with Serilog.Settings.Configuration, so it can be configured from appsettings.json (or any other Microsoft.Extensions.Configuration provider) instead of in code. Because JSON has a limited type system, a few parameters are expressed differently than their CLR types - those are called out in the table below.

Parameters

Parameter CLR type JSON type Default Notes
uri string string - (required) Absolute http/https URI; validated at startup
labels LokiLabel[] array of objects [] Each object is { "key": "...", "value": "..." }
propertiesAsLabels string[] array of strings [] Property names promoted to stream labels
propertiesAsStructuredMetadata string[] array of strings [] Property names attached as per-line structured metadata - see Structured metadata
handleLogLevelAsLabel bool boolean true Adds the level label - see Visualizing log level in Grafana
credentials LokiCredentials object null Basic-auth credentials, e.g. { "login": "...", "password": "..." }
tenant string string null Sets the X-Scope-OrgID header; validated at startup - see Authentication and multi-tenancy
traceIdMode LokiFieldDestination string None None / Body / StructuredMetadata - see Trace and span enrichment
spanIdMode LokiFieldDestination string None None / Body / StructuredMetadata - see Trace and span enrichment
batchSizeLimit int integer 1000 Max events per HTTP POST
queueLimit int integer 50000 Bounded in-memory queue; newest events dropped when full
period TimeSpan? string 1 s Flush interval; JSON value is an "hh:mm:ss" string (see notes)
eagerlyEmitFirstEvent bool boolean true Flush immediately on the first event
retryTimeLimit TimeSpan? string 10 min Stop retrying a failed batch after this; JSON value is an "hh:mm:ss" string (see notes)
restrictedToMinimumLevel LogEventLevel string Verbose e.g. "Information" (see Enum.Parse)
textFormatter ITextFormatter string LokiJsonTextFormatter Type name "My.Namespace.MyType, MyAssembly" (see notes)
exceptionFormatter ILokiExceptionFormatter string LokiExceptionFormatter Type name "My.Namespace.MyType, MyAssembly" (see Custom exception formatting)
httpClient HttpClient - null Code-only; not configurable from JSON
httpMessageHandler HttpMessageHandler - null Code-only; not configurable from JSON

Notes

  • period / retryTimeLimit are TimeSpan? in code (e.g. period: TimeSpan.FromSeconds(5)); in JSON they are written as "hh:mm:ss" strings (e.g. "00:00:05"), which the settings binder converts to TimeSpan. Omit them to use the defaults.
  • textFormatter / exceptionFormatter are bound by assembly-qualified type name and require a public parameterless constructor (LokiJsonTextFormatter and LokiExceptionFormatter both have one).
  • httpClient / httpMessageHandler are runtime objects and can only be supplied in code (for example via IHttpClientFactory). See Custom HttpClient.

Full example

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Grafana.Loki"
    ],
    "MinimumLevel": {
      "Default": "Debug"
    },
    "WriteTo": [
      {
        "Name": "GrafanaLoki",
        "Args": {
          "uri": "http://localhost:3100",
          "labels": [
            {
              "key": "app",
              "value": "web_app"
            }
          ],
          "propertiesAsLabels": [
            "app"
          ],
          "tenant": "my-tenant",
          "propertiesAsStructuredMetadata": [ "RequestId" ],
          "traceIdMode": "StructuredMetadata",
          "batchSizeLimit": 500,
          "period": "00:00:02"
        }
      }
    ]
  }
}

Migrating from v8

Several argument names changed in v9 (for example batchPostingLimitbatchSizeLimit), and a few were removed. See Upgrading from v8 to v9.

Clone this wiki locally