Skip to content
Simple .NET logging with fully-structured events
Branch: dev
Clone or download
Pull request Compare This branch is 157 commits behind serilog:dev.
tsimbalar and nblumhardt Support for LevelSwith in KeyValuePairSettings (serilog#1020)
* add support for declaring a named LoggingLevelSwitch

key="level-switch:foo" value="Information" will create an instance of LoggingLevelSwitch named foo with initial level Information

* add support for using a previously declared LoggingLevelSwitch to control MinimumLevel

key="minimum-level:controlled-by" value="foo" will set MinimumLevel.ControlledBy with the previously declared named level-switch

* [WIP] unit test for passing controlLevelSwitch to a sink

* [Refactoring] extracted methods to make it clearer

KeyValuePairSettings.ParseVariableDeclarationDirectives and KeyValuePairSettings.LookUpVariable

* add the possibility to pass a declared LoggingLevelSwitch to a sink

Passing a parameter directive the name of a declared Switch will pass it to the configuration method

* add support for level-switch:Switch1 - value = "" and interpret it as level=information

* Minor edits/typos in the unit tests

* Change brittle tests in order not to rely on reflection to acces brittle private field

* Simplify the code around "creating a new LoggingLevelSwitch"
+ throw a "nicer" error message when referencing an undeclared LoggingLevelSwitch

* Add support for referencing a LoggingLevelSwitch from a minimul level override

key="serilog:minimum-level:override:System" value ="switchName" will bind the override to a switch that may be controlled through the sink

* Enforce declaration of switch as level-switch:$switchName
... and use $ also when referencing the declared swith
This helps disambiguate between a variable and the minimum level for a switch ... and makes sure noone is goind to declared a switch with name "Information"

* Minor tweaks after review

* Explicitly throw a helpful FormatException when specifiying a level-switch with a switch name not starting with a $ sign

* Minor refactoring of ParseNamedLevelSwitchDeclarationDirectives
use Dictionary.Add() instead of Dictionary[] to make it mor explicit that uniqueness is implied

* Refactoring : use TryGetValue instead of ContainsKey

* Refactoring : renamed LookUpSwitchByNameOrThrow to LookUpSwitchByName for consistency
Latest commit 3bb8523 Sep 20, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Fixed dead link to CONTRIBUTING.md Sep 16, 2017
assets Removed CommonAssemblyInfo, it's not used anymore Dec 2, 2015
results less allocations Jun 6, 2017
src/Serilog Support for LevelSwith in KeyValuePairSettings (serilog#1020) Sep 20, 2017
test Support for LevelSwith in KeyValuePairSettings (serilog#1020) Sep 20, 2017
.editorconfig Some cleanup Jun 2, 2016
.gitattributes Added gitattributes to set CRLF to auto. Apr 8, 2013
.gitignore Improve travis(osx/linux) build Mar 4, 2017
.travis.yml Update to use edge group. Jun 21, 2017
Build.ps1 Preserve informational version when building on master Aug 30, 2017
BuildNativeUWP.ps1 rename: test/Serilog.UWP -> test/Serilog.UwpTests Jul 4, 2017
CHANGES.md Updated Changes with 2.5.0 release [Skip CI] Jun 21, 2017
CODE_OF_CONDUCT.md Removed detailed code in favour of Contributor Covenant link. Sep 4, 2017
CONTRIBUTING.md Grammar fix [Skip CI] Sep 13, 2017
Directory.Build.props enable .net native toolchain for ci Jul 3, 2017
LICENSE GitHub Apache 2 licence template Oct 11, 2013
NuGet.config Some cleanup Jun 2, 2016
README.md Removed detailed code in favour of Contributor Covenant link. Sep 4, 2017
RunPerfTests.ps1 Updated (micro) benchmarks Aug 19, 2016
Serilog.sln Use magic sln project guids, Mar 8, 2017
Serilog.sln.DotSettings Namespace layout and RESHARPER GREEN! Jun 1, 2017
appveyor-perftest.yml Independent Build for Perf Tests. Jun 2, 2017
appveyor.yml enable .net native toolchain for ci Jul 3, 2017
build.cmd Fix travis build, remove OpenCover Mar 4, 2017
build.sh Removed install from build script and rely on Travis yml Jun 3, 2017
global.json Use dotnet core sdk 1.0.1 Mar 9, 2017
run_perf_tests.sh Fix `run_perf_tests.sh` Mar 8, 2017

README.md

Serilog Build status NuGet Version Join the chat at https://gitter.im/serilog/serilog 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.

var log = new LoggerConfiguration()
    .WriteTo.LiterateConsole()
    .WriteTo.RollingFile("log-{Date}.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 Serilog 1.x? Check out the 2.0 Upgrade Guide and Release Notes.

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 a provider for Microsoft.Extensions.Logging
  • 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 "literate" console sink, and a rolling file set:

Install-Package Serilog
Install-Package Serilog.Sinks.Literate
Install-Package Serilog.Sinks.RollingFile

The simplest way to set up Serilog is using the static Log class. A LoggerConfiguration is used to create and assign the default logger.

public class Program
{
    public static void Main()
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.LiterateConsole()
            .WriteTo.RollingFile("log-{Date}.txt")
            .CreateLogger();
    }
}

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:

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. 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 Travis
dev Build status Build Status
master Build status Build Status

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

You can’t perform that action at this time.