• Stars
    star
    60
  • Rank 503,336 (Top 10 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

Deprecated: new applications should use https://github.com/serilog/serilog-sinks-file instead

Serilog.Sinks.RollingFile Build status NuGet Version Documentation Join the chat at https://gitter.im/serilog/serilog

Writes Serilog events to a set of text files, one per day.

⚠️ Deprecation notice: the rolling functionality in this sink has been improved and merged into the Serilog.Sinks.File package.

Getting started

Install the Serilog.Sinks.RollingFile package from NuGet:

Install-Package Serilog.Sinks.RollingFile

To configure the sink in C# code, call WriteTo.RollingFile() during logger configuration:

var log = new LoggerConfiguration()
    .WriteTo.RollingFile("log-{Date}.txt")
    .CreateLogger();

Log.Information("This will be written to the rolling file set");

The filename should include the {Date} placeholder, which will be replaced with the date of the events contained in the file. Filenames use the yyyyMMdd date format so that files can be ordered using a lexicographic sort:

log-20160630.txt
log-20160701.txt
log-20160702.txt

Important: By default, only one process may write to a log file at a given time. See Shared log files below for information on multi-process sharing.

Limits

To avoid bringing down apps with runaway disk usage the rolling file sink limits file size to 1GB by default. The limit can be changed or removed using the fileSizeLimitBytes parameter.

    .WriteTo.RollingFile("log-{Date}.txt", fileSizeLimitBytes: null)

For the same reason, only the most recent 31 files are retained by default (i.e. one long month). To change or remove this limit, pass the retainedFileCountLimit parameter.

    .WriteTo.RollingFile("log-{Date}.txt", retainedFileCountLimit: null)

XML <appSettings> configuration

To use the rolling file 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 rolling file sink assembly and required path format under the <appSettings> node:

<configuration>
  <appSettings>
    <add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
    <add key="serilog:write-to:RollingFile.pathFormat" value="log-{Date}.txt" />

The parameters that can be set through the serilog:write-to:RollingFile keys are the method parameters accepted by the WriteTo.RollingFile() configuration method. This means, for example, that the fileSizeLimitBytes parameter can be set with:

    <add key="serilog:write-to:RollingFile.fileSizeLimitBytes" value="1234567" />

Omitting the value will set the parameter to null:

    <add key="serilog:write-to:RollingFile.fileSizeLimitBytes" />

In XML and JSON configuration formats, environment variables can be used in setting values. This means, for instance, that the log file path can be based on TMP or APPDATA:

    <add key="serilog:write-to:RollingFile.pathFormat" value="%APPDATA%\MyApp\log-{Date}.txt" />

JSON appsettings.json configuration

To use the rolling file 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 rolling file 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": "RollingFile", "Args": { "pathFormat": "log-{Date}.txt" } }
    ]
  }
}

See the XML <appSettings> example above for a discussion of available Args options.

Controlling event formatting

The rolling file sink creates events in a fixed text format by default:

2016-07-06 09:02:17.148 +10:00 [Information] HTTP "GET" "/" responded 200 in 1994 ms

The format is controlled using an output template, which the rolling file configuration method accepts as an outputTemplate parameter.

The default format above corresponds to an output template like:

    .WriteTo.RollingFile("log-{Date}.txt",
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}")
JSON event formatting

To write events to the file in an alternative format such as JSON, pass an ITextFormatter as the first argument:

    .WriteTo.RollingFile(new JsonFormatter(), "log-{Date}.txt")

Shared log files

To enable multi-process shared log files, set shared to true:

    .WriteTo.RollingFile("log-{Date}.txt", shared: true)

Filename format specifiers

The sink supports three different filename format specifiers:

  • {Date} Creates a file per day. Filenames use the yyyyMMdd format.
  • {Hour} Creates a file per hour. Filenames use the yyyyMMddHH format.
  • {HalfHour} Creates a file per half hour. Filenames use the yyyyMMddHHmm format.

If a log file path is provided without one of the specifiers above, {Date} will be inserted by default.

Performance

By default, the rolling file sink will flush each event written through it to disk. To improve write performance, specifying buffered: true will permit the underlying stream to buffer writes.

The Serilog.Sinks.Async package can be used to wrap the rolling file sink and perform all disk access on a background worker thread.

Alternatives

The default rolling file sink is designed to suit most applications. So that we can keep it maintainable and reliable, it does not provide a large range of optional behavior. Check out alternative implemementations like this one if your needs aren't met by the default version.

Copyright © 2016 Serilog Contributors - Provided under the Apache License, Version 2.0.

More Repositories

1

serilog

Simple .NET logging with fully-structured events
C#
7,150
star
2

serilog-aspnetcore

Serilog integration for ASP.NET Core
C#
1,306
star
3

serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
C#
443
star
4

serilog-sinks-file

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

serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
C#
312
star
6

serilog-sinks-console

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

serilog-sinks-async

An asynchronous wrapper for Serilog sinks that logs on a background thread
C#
231
star
8

serilog-expressions

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

serilog-sinks-mssqlserver

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

serilog-extensions-logging-file

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

serilog-formatting-compact

Compact JSON event format for Serilog
C#
153
star
12

serilog-extensions-hosting

Serilog logging for Microsoft.Extensions.Hosting
C#
139
star
13

serilog-sinks-opentelemetry

A Serilog OpenTelemetry Protocol (OTLP) sink
C#
116
star
14

serilog-filters-expressions

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

serilog-enrichers-environment

Enrich Serilog log events with properties from System.Environment.
C#
77
star
16

serilog-sinks-email

A Serilog sink that writes events to SMTP email
C#
72
star
17

serilog-sinks-periodicbatching

Infrastructure for Serilog sinks that process events in batches.
C#
70
star
18

serilog-sinks-browserconsole

A console sink for the Blazor/Wasm environment
C#
61
star
19

serilog-sinks-xamarin

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

serilog-sinks-eventlog

A Serilog sink that writes events to the Windows Event Log
C#
50
star
21

serilog-settings-appsettings

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

serilog-enrichers-thread

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

serilog-sinks-map

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

serilog-sinks-debug

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

serilog-formatting-compact-reader

A reader for Serilog's compact JSON format
C#
32
star
26

serilog-sinks-loggly

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

serilog-enrichers-process

The process enricher for Serilog.
C#
26
star
28

serilog-sinks-observable

Write Serilog events to observers (Rx) through an IObservable
C#
23
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-trace

The diagnostic trace sink for Serilog.
C#
11
star
34

serilog-sinks-coloredconsole

Deprecated: now a part of https://github.com/serilog/serilog-sinks-console
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