Author : MD TAREQ HASSAN
Rolling File Feature
Serilog.Sinks.RollingFile
is deprecated- Rolling file features have been incorporated into
Serilog.Sinks.File
Nuget package
Installation
Install-Package Serilog.Sinks.File
Install-Package Serilog.Settings.Configuration
Serilog Configuration
Fluent API
var log = new LoggerConfiguration()
.WriteTo.File(path: "log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
From appsettings
Nuget package
Install-Package Serilog.Settings.Configuration
appsettings.json
{
"Serilog": {
"Using": ["Serilog.Sinks.File"],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "",
"rollingInterval": "Day"
}
}
]
}
}
Create configuration from appsettings
//
// Configuration via DI or create manually
//
//var configuration = new ConfigurationBuilder()
// .AddJsonFile("appsettings.json")
// .Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Rolling policies
Fluent API
var log = new LoggerConfiguration()
.WriteTo.File(
path: "log.txt",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: null,
rollOnFileSizeLimit: true,
)
.CreateLogger();
appsettings.json
{
"Serilog": {
"Using": ["Serilog.Sinks.File"],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "",
"fileSizeLimitBytes": "",
"rollOnFileSizeLimit": "",
"rollingInterval": "Day"
}
},
]
}
}
Retained File Count Limit
- Setting:
"retainedFileCountLimit" : ""
- Default value for
retainedFileCountLimit
: 31 files
appsettings.json
{
"Serilog": {
"Using": ["Serilog.Sinks.File"],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "",
"fileSizeLimitBytes": "",
"rollOnFileSizeLimit": "",
"rollingInterval": "",
"retainedFileCountLimit": null, // default: 31 files
}
},
]
}
}
Fluent API
var log = new LoggerConfiguration()
.WriteTo.File(
path: "log.txt",
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: null
)
.CreateLogger();