• Stars
    star
    6,857
  • Rank 5,461 (Top 0.2 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple .NET logging with fully-structured events

Serilog Build status NuGet Version NuGet Downloads Stack Overflow

Serilog is a diagnostic logging library for .NET applications. It is easy to set up, has a clean API, and runs on all recent .NET platforms. While it's useful even in the simplest applications, Serilog's support for structured logging shines when instrumenting complex, distributed, and asynchronous applications and systems.

Serilog

Like many other libraries for .NET, Serilog provides diagnostic logging to files, the console, and many other outputs.

using var log = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("log.txt")
    .CreateLogger();

log.Information("Hello, Serilog!");

Unlike other logging libraries, Serilog is built from the ground up to record structured event data.

var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 34;

log.Information("Processed {@Position} in {Elapsed} ms", position, elapsedMs);

Serilog uses message templates, a simple DSL that extends .NET format strings with named as well as positional parameters. Instead of formatting events immediately into text, Serilog captures the values associated with each named parameter.

The example above records two properties, Position and Elapsed, in the log event. The @ operator in front of Position tells Serilog to serialize the object passed in, rather than convert it using ToString(). Serilog's deep and rich support for structured event data opens up a huge range of diagnostic possibilities not available when using traditional loggers.

Rendered into JSON format for example, these properties appear alongside the timestamp, level, and message like:

{"Position": {"Latitude": 25, "Longitude": 134}, "Elapsed": 34}

Back-ends that are capable of recording structured event data make log searches and analysis possible without log parsing or regular expressions.

Supporting structured data doesn't mean giving up text: when Serilog writes events to files or the console, the template and properties are rendered into friendly human-readable text just like a traditional logging library would produce:

09:14:22 [INF] Processed {"Latitude": 25, "Longitude": 134} in 34 ms.

Upgrading from an earlier Serilog version? Find release notes here.

Features

  • Community-backed and actively developed
  • Format-based logging API with familiar levels like Debug, Information, Warning, Error, and so-on
  • Discoverable C# configuration syntax and optional XML or JSON configuration support
  • Efficient when enabled, extremely low overhead when a logging level is switched off
  • Best-in-class .NET Core support, including rich integration with ASP.NET Core
  • Support for a comprehensive range of sinks, including files, the console, on-premises and cloud-based log servers, databases, and message queues
  • Sophisticated enrichment of log events with contextual information, including scoped (LogContext) properties, thread and process identifiers, and domain-specific correlation ids such as HttpRequestId
  • Zero-shared-state Logger objects, with an optional global static Log class
  • Format-agnostic logging pipeline that can emit events in plain text, JSON, in-memory LogEvent objects (including Rx pipelines) and other formats

Getting started

Serilog is installed from NuGet. To view log events, one or more sinks need to be installed as well, here we'll use the pretty-printing console sink, and a rolling file set:

dotnet add package Serilog
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.File

The simplest way to set up Serilog is using the static Log class. A LoggerConfiguration is used to create and assign the default logger, normally in Program.cs:

using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("log.txt",
        rollingInterval: RollingInterval.Day,
        rollOnFileSizeLimit: true)
    .CreateLogger();

try
{
    // Your program here...
    const string name = "Serilog";
    Log.Information("Hello, {Name}!", name);
    throw new InvalidOperationException("Oops...");
}
catch (Exception ex)
{
    Log.Error(ex, "Unhandled exception");
}
finally
{
    await Log.CloseAndFlushAsync(); // ensure all logs written before app exits
}

Find more, including a runnable example application, under the Getting Started topic in the documentation.

Getting help

To learn more about Serilog, check out the documentation - you'll find information there on the most common scenarios. If Serilog isn't working the way you expect, you may find the troubleshooting guide useful.

Serilog has an active and helpful community who are happy to help point you in the right direction or work through any issues you might encounter. You can get in touch via:

We welcome reproducible bug reports and detailed feature requests through our GitHub issue tracker; note the other resource are much better for quick questions or seeking usage help.

Contributing

Would you like to help make Serilog even better? We keep a list of issues that are approachable for newcomers under the up-for-grabs label (accessible only when logged into GitHub). Before starting work on a pull request, we suggest commenting on, or raising, an issue on the issue tracker so that we can help and coordinate efforts. For more details check out our contributing guide.

When contributing please keep in mind our Code of Conduct.

Detailed build status

Branch AppVeyor
dev Build status
main Build status

Serilog is copyright Β© Serilog Contributors - Provided under the Apache License, Version 2.0. Needle and thread logo a derivative of work by Kenneth Appiah.

More Repositories

1

serilog-aspnetcore

Serilog integration for ASP.NET Core
C#
1,250
star
2

serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
C#
418
star
3

serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
C#
314
star
4

serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
C#
301
star
5

serilog-sinks-console

Write log events to System.Console as text or JSON, with ANSI theme support
C#
223
star
6

serilog-sinks-async

An asynchronous wrapper for Serilog sinks that logs on a background thread
C#
212
star
7

serilog-expressions

An embeddable mini-language for filtering, enriching, and formatting Serilog events, ideal for use with JSON or XML configuration.
C#
170
star
8

serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server
C#
169
star
9

serilog-extensions-logging-file

Add file logging to ASP.NET Core apps in one line of code.
C#
148
star
10

serilog-formatting-compact

Compact JSON event format for Serilog
C#
139
star
11

serilog-extensions-hosting

Serilog logging for Microsoft.Extensions.Hosting
C#
123
star
12

serilog-sinks-opentelemetry

Serilog to OpenTelemetry Logs sink
C#
87
star
13

serilog-filters-expressions

Expression-based event filtering for Serilog
C#
80
star
14

serilog-enrichers-environment

Enrich Serilog log events with properties from System.Environment.
C#
71
star
15

serilog-sinks-email

A Serilog sink that writes events to SMTP email
C#
68
star
16

serilog-sinks-periodicbatching

Infrastructure for Serilog sinks that process events in batches.
C#
65
star
17

serilog-sinks-browserconsole

A console sink for the Blazor/Wasm environment
C#
60
star
18

serilog-sinks-rollingfile

Deprecated: new applications should use https://github.com/serilog/serilog-sinks-file instead
C#
59
star
19

serilog-sinks-xamarin

A Serilog sink that writes events to Xamarin mobile targets
C#
53
star
20

serilog-settings-appsettings

An <appSettings> configuration reader for Serilog
C#
50
star
21

serilog-sinks-eventlog

A Serilog sink that writes events to the Windows Event Log
C#
44
star
22

serilog-enrichers-thread

Enrich Serilog events with properties from the current thread.
C#
40
star
23

serilog-sinks-map

A Serilog sink wrapper that dispatches events based on a property value
C#
39
star
24

serilog-sinks-debug

Writes Serilog events to the debug output window
C#
31
star
25

serilog-sinks-loggly

A Serilog event sink that writes to Loggly
C#
27
star
26

serilog-formatting-compact-reader

A reader for Serilog's compact JSON format
C#
25
star
27

serilog-enrichers-process

The process enricher for Serilog.
C#
24
star
28

serilog-sinks-observable

Write Serilog events to observers (Rx) through an IObservable
C#
18
star
29

serilog-sinks-azuredocumentdb

A Serilog sink that writes to Azure DocumentDB
C#
17
star
30

serilog-sinks-amazonkinesis

A Serilog sink that logs to Amazon Kinesis
C#
14
star
31

serilog-sinks-signalr

A Serilog sink that writes events to SignalR
C#
14
star
32

serilog-sinks-log4net

A Serilog sink that writes events to log4net
C#
13
star
33

serilog-sinks-coloredconsole

Deprecated: now a part of https://github.com/serilog/serilog-sinks-console
C#
10
star
34

serilog-sinks-trace

The diagnostic trace sink for Serilog.
C#
10
star
35

serilog-sinks-datadog

A Serilog sink that writes events to DataDog
C#
9
star
36

serilog-sinks-logentries

A Serilog sink that writes events to Logentries
C#
8
star
37

serilog-sinks-nlog

A Serilog sink that writes events to NLog
C#
8
star
38

serilog-sinks-textwriter

The System.IO.TextWriter sink for Serilog
C#
8
star
39

serilog-generator

A simulation that generates simple log data through Serilog, ideal for testing sinks or log servers
C#
6
star
40

serilog-sinks-rethinkdb

A Serilog sink that writes to RethinkDB
C#
5
star
41

serilog-sinks-xsockets

A Serilog sink that writes events to XSockets
C#
4
star
42

serilog-sinks-reflectinsight

Writes events from Serilog to the ReflectInsight logging framework
C#
3
star
43

serilog-dnx-prerelease

Pre-release support for the DNX (.NET 5) runtime environment for Serilog
C#
3
star
44

serilog-sinks-dynamodb

A Serilog sink that writes events to Amazon Web Services DynamoDB
C#
1
star