Author : MD TAREQ HASSAN

Rolling File Feature

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

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();