Serilog.Sinks.AsyncFile 3.0.0

dotnet add package Serilog.Sinks.AsyncFile --version 3.0.0
                    
NuGet\Install-Package Serilog.Sinks.AsyncFile -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.AsyncFile" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Sinks.AsyncFile" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Serilog.Sinks.AsyncFile" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Serilog.Sinks.AsyncFile --version 3.0.0
                    
#r "nuget: Serilog.Sinks.AsyncFile, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Serilog.Sinks.AsyncFile@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Serilog.Sinks.AsyncFile&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Serilog.Sinks.AsyncFile&version=3.0.0
                    
Install as a Cake Tool

Serilog.Sinks.AsyncFile

An advanced file sink for Serilog that writes events to files in plain text.

Features

  • Embedded asynchronous log writing using the Channel<T> api
  • Control over the channel capacity
  • Advanced rolling policies
    • Rolling by file size
    • Rolling on startup
    • Change the rolled file name format
    • Roll to archive folder
    • Delete old rolled files by age

More features and ideas will be welcomed! Just log an issue.

Getting started

Install the Serilog.Sinks.AsyncFile package from NuGet:

dotnet add package Serilog.Sinks.AsyncFile

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

var log = new LoggerConfiguration()
    .WriteTo.AsyncFile("log.txt")
    .CreateLogger();

This will append the time period to the filename, creating a file set like:

log.txt
2024-11-18-20-39-25_log.txt
2024-11-18-20-47-41_log.txt
2024-11-18-20-56-12_log.txt

Rolling policies

Rolling by file size

The file sink can roll files based on a maximum size in bytes. The default value if not specify is 50 MB. To roll files when they reach a certain size, specify RollingPolicyOptions.FileSizeLimitBytes:

    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        FileSizeLimitBytes = 100 * 1024 * 1024
    })

This will create a new file when the current one reaches 100 MB.

Rolling on startup

The file sink can roll files on startup. To roll files when the application starts, specify RollingPolicyOptions.RollOnStartup:

    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        RollOnStartup = true
    })

This will create a new file when the application starts.

Change the rolled file name format

The sink can roll files with a specific file name format. To roll files with a specific file name format, specify RollingPolicyOptions.RollingFileNameFormat (Default: "{0:yyyy-MM-dd-HH-mm-ss}_{1}{2}"):

  • 0: The timestamp of the log event
  • 1: The original file name without extension
  • 2: The original file extension (with the dot)
    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        RollingFileNameFormat = "{0:MM-dd-yyyy-HH-mm-ss}_{1}.archived{2}"
    })
Roll to archive folder

The file sink can roll files to an archive folder. To roll files to an archive folder, specify RollingPolicyOptions.RollToArchiveFolder (Default: false) and RollingPolicyOptions.ArchiveFolderName (Default: "Archive"):

    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        RollToArchiveFolder = true,
        ArchiveFolderName = "old"
    })

This will create a new file in the archive folder when the current one reaches the maximum size.

Delete old rolled files by age

The file sink can delete old rolled files by age. To delete old rolled files by age, specify RollingPolicyOptions.RollingRetentionDays (Default: 14):

    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        RollingRetentionDays = 30
    })

This will delete old rolled files by age.

Age check interval

The file sink can check for old rolled files in logs or archive folder by age at a specified interval. To check for old rolled files by age at a specified interval, specify RollingPolicyOptions.AgeCheckInterval (Default: 1 hour):

    .WriteTo.AsyncFile("log.txt", new RollingPolicyOptions
    {
        AgeCheckInterval = TimeSpan.FromHours(2)
    })

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.AsyncFile("log.txt",
    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")

Copyright © 2024 by Yoav Melamed - Provided under the Apache License, Version 2.0.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 98 8/14/2025
2.1.0 149 7/14/2025
2.0.0 366 12/9/2024
1.0.2 338 11/22/2024
1.0.1 123 11/21/2024
1.0.0 125 11/18/2024

- Output target framework is now net8.0, net9.0, netstandard2.0, and netstandard2.1 (instead of net6.0 and 7.0).
           - Fix issue when file was not alwats archived when it should have been.