Serilog.Sinks.FileEx
5.1.8
dotnet add package Serilog.Sinks.FileEx --version 5.1.8
NuGet\Install-Package Serilog.Sinks.FileEx -Version 5.1.8
<PackageReference Include="Serilog.Sinks.FileEx" Version="5.1.8" />
paket add Serilog.Sinks.FileEx --version 5.1.8
#r "nuget: Serilog.Sinks.FileEx, 5.1.8"
// Install Serilog.Sinks.FileEx as a Cake Addin #addin nuget:?package=Serilog.Sinks.FileEx&version=5.1.8 // Install Serilog.Sinks.FileEx as a Cake Tool #tool nuget:?package=Serilog.Sinks.FileEx&version=5.1.8
Serilog.Sinks.FileEx
Writes Serilog events to one or more text files.
Getting started
Install the Serilog.Sinks.FileEx package from NuGet:
Install-Package Serilog.Sinks.FileEx
To configure the sink in C# code, call WriteTo.FileEx()
during logger configuration:
var log = new LoggerConfiguration()
.WriteTo.FileEx("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
This will append the time period to the filename, creating a file set like:
log20180631.txt
log20180701.txt
log20180702.txt
If you want to preserve filename when rolling the logs, so it's always the filename that gets written to. It's mostly useful for other tools like fail2ban and elastic filebeat to be able to continuously read the log. Set the parameter preserveLogFileName to true. log.txt will always have the latest logs, content will be copied to a new file and then flushed on file rolling.
var log = new LoggerConfiguration()
.WriteTo.FileEx("log.txt", rollingInterval: RollingInterval.Day, preserveLogFileName: true)
.CreateLogger();
This will preserve the log file like:
log.txt
log20180631.txt
log20180701.txt
log20180702.txt
If you want to configure a custom rollInterval date format, Set the parameter periodFormat to a date format string.
var log = new LoggerConfiguration()
.WriteTo.FileEx("log.txt", "_yyyy-MM-dd", rollingInterval: RollingInterval.Day)
.CreateLogger();
This will append the date and time period to the filename using the custom format, create a log set like:
log_2018-06-31.txt
log_2018-07-01.txt
log_2018-07-02.txt
If you want to create a new log file on every application startup, Set the parameter rollOnEachProcessRun to true.a new log file will create and roll the latest one on every application startup.
var log = new LoggerConfiguration()
.WriteTo.FileEx("log.txt", rollOnEachProcessRun: true)
.CreateLogger();
This will create a new log file on every application startup, create a log set like:
log_001.txt
log_002.txt
log_003.txt
If you want to use the log file last write time as the rolling date and time instead of the checkpoint, Set the parameter useLastWriteAsTimestamp to true.
var log = new LoggerConfiguration()
.WriteTo.FileEx("log.txt", "_yyyy-MM-dd", rollingInterval: RollingInterval.Day, useLastWriteAsTimestamp: true)
.CreateLogger();
This will append the last write time for the datetime format, create a log set like:
log_2018-06-31.txt
log_2018-07-01.txt
log_2018-07-02.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 file sink limits file size to 1GB by default. Once the limit is reached, no further events will be written until the next roll point (see also: Rolling policies below).
The limit can be changed or removed using the fileSizeLimitBytes
parameter.
.WriteTo.FileEx("log.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.FileEx("log.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: null)
Rolling policies
To create a log file per day or other time period, specify a rollingInterval
as shown in the examples above.
To roll when the file reaches fileSizeLimitBytes
, specify rollOnFileSizeLimit
:
.WriteTo.FileEx("log.txt", rollOnFileSizeLimit: true)
This will create a file set like:
log.txt
log_001.txt
log_002.txt
Specifying both rollingInterval
and rollOnFileSizeLimit
will cause both policies to be applied, while specifying neither will result in all events being written to a single file.
Old files will be cleaned up as per retainedFileCountLimit
- the default is 31.
XML <appSettings>
configuration
To use the 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 file sink assembly and required path format under the <appSettings>
node:
<configuration>
<appSettings>
<add key="serilog:using:FileEx" value="Serilog.Sinks.FileEx" />
<add key="serilog:write-to:FileEx.path" value="log.txt" />
The parameters that can be set through the serilog:write-to:FileEx
keys are the method parameters accepted by the WriteTo.FileEx()
configuration method. This means, for example, that the fileSizeLimitBytes
parameter can be set with:
<add key="serilog:write-to:FileEx.fileSizeLimitBytes" value="1234567" />
Omitting the value
will set the parameter to null
:
<add key="serilog:write-to:FileEx.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:FileEx.path" value="%APPDATA%\MyApp\log.txt" />
JSON appsettings.json
configuration
To use the 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 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": "FileEx", "Args": { "path": "log.txt", "rollingInterval": "Day" } }
]
}
}
See the XML <appSettings>
example above for a discussion of available Args
options.
Controlling event formatting
The file sink creates events in a fixed text format by default:
2018-07-06 09:02:17.148 +10:00 [INF] HTTP GET / responded 200 in 1994 ms
The format is controlled using an output template, which the file configuration method accepts as an outputTemplate
parameter.
The default format above corresponds to an output template like:
.WriteTo.FileEx("log.txt",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{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:
// Install-Package Serilog.Formatting.Compact
.WriteTo.FileEx(new CompactJsonFormatter(), "log.txt")
Shared log files
To enable multi-process shared log files, set shared
to true
:
.WriteTo.FileEx("log.txt", shared: true)
Auditing
The file sink can operate as an audit file through AuditTo
:
.AuditTo.FileEx("audit.txt")
Only a limited subset of configuration options are currently available in this mode.
Performance
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 access on a background worker thread.
Extensibility
FileLifecycleHooks
provide an extensibility point that allows hooking into different parts of the life cycle of a log file.
You can create a hook by extending from FileLifecycleHooks
and overriding the OnFileOpened
and/or OnFileDeleting
and/or OnFileRolling
and/or OnFileRolled
methods.
OnFileOpened
provides access to the underlying stream that log events are written to, before Serilog begins writing events. You can use this to write your own data to the stream (for example, to write a header row), or to wrap the stream in another stream (for example, to add buffering, compression or encryption)OnFileDeleting
provides a means to work with obsolete rolling log files, before they are deleted by Serilog's retention mechanism - for example, to archive log files to another locationOnFileRolling
provides a means to work with rolling log files, before they are rolled by Serilog's retention mechanism - for example, to archive log files to another locationOnFileRolled
provides a means to work with rolling log files, after they are rolled by Serilog's retention mechanism - for example, to archive log files to another
Available hooks:
- serilog-sinks-file-gzarchive: compresses logs as they are written, using streaming GZIP compression and archives obsolete rolling log files before they are deleted and while rolling by Serilog's retention mechanism
Copyright © 2023 Ashkan Shirian and Serilog Contributors - Provided under the Apache License, Version 2.0.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard1.5 is compatible. netstandard1.6 is compatible. netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Serilog (>= 2.12.0)
-
.NETStandard 1.5
- NETStandard.Library (>= 1.6.1)
- Serilog (>= 2.12.0)
-
.NETStandard 1.6
- NETStandard.Library (>= 1.6.1)
- Serilog (>= 2.12.0)
- System.IO.Compression (>= 4.3.0)
-
.NETStandard 2.0
- Serilog (>= 2.12.0)
-
.NETStandard 2.1
- Serilog (>= 2.12.0)
-
net5.0
- Serilog (>= 2.12.0)
-
net6.0
- Serilog (>= 2.12.0)
-
net7.0
- Serilog (>= 2.12.0)
-
net8.0
- Serilog (>= 2.12.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Serilog.Sinks.FileEx:
Package | Downloads |
---|---|
Serilog.Sinks.File.GzArchive
Plugin for the Serilog File sink that archives completed log files, optionally compressing them. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
5.1.8 | 1,963 | 12/14/2023 |
5.1.7 | 264 | 11/14/2023 |
5.1.6 | 2,642 | 7/16/2023 |
5.1.5 | 235 | 7/9/2023 |
5.1.4 | 301 | 6/25/2023 |
5.1.3 | 218 | 6/25/2023 |
5.1.2 | 202 | 6/18/2023 |
5.1.1 | 208 | 6/17/2023 |
5.1.0 | 195 | 6/16/2023 |
5.0.6 | 197 | 6/16/2023 |
5.0.5 | 221 | 6/14/2023 |
5.0.4 | 136 | 6/14/2023 |
5.0.3 | 150 | 6/14/2023 |
5.0.2 | 143 | 6/13/2023 |
5.0.1 | 223 | 6/13/2023 |
- Added DotNet 8.0 support