Writes Serilog events to a text file.
var log = new LoggerConfiguration()
.WriteTo.File("log.txt")
.CreateLogger();
To avoid bringing down apps with runaway disk usage the file sink limits file size to 1GB by default. The limit can be increased or removed using the fileSizeLimitBytes
parameter.
.WriteTo.File("log.txt", fileSizeLimitBytes: null)
Important: By default only one process may use a log file at a given time. See Shared log files below if multi-process logging is required.
The sink can be configured in XML app-settings format if the Serilog.Settings.AppSettings package is in use:
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:write-to:File.path" value="log.txt" />
<add key="serilog:write-to:File.fileSizeLimitBytes" value="" />
To emit JSON, rather than plain text, a formatter can be specified:
.WriteTo.File(new JsonFormatter(), "log.txt")
To configure an alternative formatter in XML <appSettings>
, specify the formatter's assembly-qualified type name as the setting value
.
Multiple processes can concurrently write to the same log file if the shared
parameter is set to true
:
.WriteTo.File("log.txt", shared: true)
By default, the 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 file sink and perform all disk accss on a background worker thread.
Copyright © 2016 Serilog Contributors - Provided under the Apache License, Version 2.0.