A simple Serilog sink that writes log events as text to STDOUT
. For colored console output, check out Serilog.Sinks.Literate, which is designed for interactive use.
To use the console sink, first install the NuGet package:
Install-Package Serilog.Sinks.Console
Then enable the sink using WriteTo.Console()
:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
Log.Information("Hello, world!");
Log events will be printed to STDOUT
:
2016-07-08 12:50:51 [Information] Hello, world!
The format of events to the console can be modified using the outputTemplate
configuration parameter:
.WriteTo.Console(
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}")
The default template, shown in the example above, uses built-in properties like Timestamp
and Level
. Properties from events, including those attached using enrichers, can also appear in the output template.
For more compact level names, use a format such as {Level:u3}
or {Level:w3}
for three-character upper- or lowercase level names, respectively.
To write JSON instead of plain text, pass an appropriate ITextFormatter
to the Console()
configuration method:
.WriteTo.Console(new JsonFormatter())
Serilog.Formatting.Compact provides alternatives to the default JsonFormatter
.
To use the console sink with the Serilog.Settings.AppSettings package, first install that package if you haven't already done so:
Install-Package Serilog.Settings.AppSettings
Instead of configuring the logger in code, call ReadFrom.AppSettings()
:
var log = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();
In your application's App.config
or Web.config
file, specify the console sink assembly under the <appSettings>
node:
<configuration>
<appSettings>
<add key="serilog:using:Console" value="Serilog.Sinks.Console" />
<add key="serilog:write-to:Console" />
To use the console sink with Microsoft.Extensions.Configuration, for example with ASP.NET Core or .NET Core, use the Serilog.Settings.Configuration package. First install that package if you have not already done so:
Install-Package Serilog.Settings.Configuration
Instead of configuring the sink directly in code, call ReadFrom.Configuration()
:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
In your appsettings.json
file, under the Serilog
node, :
{
"Serilog": {
"WriteTo": [{"Name": "Console"}]
}
}
Copyright © 2016 Serilog Contributors - Provided under the Apache License, Version 2.0.