Serilog.Extensions.Logging.File 9.0.0-dev-02302

Prefix Reserved
This is a prerelease version of Serilog.Extensions.Logging.File.
dotnet add package Serilog.Extensions.Logging.File --version 9.0.0-dev-02302                
NuGet\Install-Package Serilog.Extensions.Logging.File -Version 9.0.0-dev-02302                
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.Extensions.Logging.File" Version="9.0.0-dev-02302" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Extensions.Logging.File --version 9.0.0-dev-02302                
#r "nuget: Serilog.Extensions.Logging.File, 9.0.0-dev-02302"                
#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.
// Install Serilog.Extensions.Logging.File as a Cake Addin
#addin nuget:?package=Serilog.Extensions.Logging.File&version=9.0.0-dev-02302&prerelease

// Install Serilog.Extensions.Logging.File as a Cake Tool
#tool nuget:?package=Serilog.Extensions.Logging.File&version=9.0.0-dev-02302&prerelease                

Serilog.Extensions.Logging.File Build status NuGet Version

This package makes it a one-liner - loggingBuilder.AddFile() - to configure top-quality file logging for ASP.NET Core apps.

  • Text or JSON file output
  • Files roll over on date; capped file size
  • Request ids and event ids included with each message
  • Log writes are performed asynchronously
  • Files are periodically flushed to disk (required for Azure App Service log collection)
  • Fast, stable, battle-proven logging code courtesy of Serilog

You can get started quickly with this package, and later migrate to the full Serilog API if you need more sophisticated log file configuration.

Versioning

This package tracks the version of ASP.NET Core that it targets. If you're using 9.x.x of ASP.NET Core, use 9.x.x of Serilog.Extensions.Logging.File, and so on.

If the version you're using doesn't have a corresponding Serilog.Extensions.Logging.File release, target v3.x of this package.

Getting started

1. Add the NuGet package as a dependency of your project either with the package manager or directly to the CSPROJ file:

<PackageReference Include="Serilog.Extensions.Logging.File" Version="9.0.0" />

2. In your Program class, configure logging on the host builder, and call AddFile() on the provided ILoggingBuilder:

builder.Services.AddLogging(logging =>
{
    logging.AddFile("Logs/myapp-{Date}.txt");
});

Done! The framework will inject ILogger instances into controllers and other classes:

class HomeController : Controller
{
    readonly ILogger<HomeController> _log;

    public HomeController(ILogger<HomeController> log)
    {
        _log = log;
    }

    public IActionResult Index()
    {
        _log.LogInformation("Hello, world!");
    }
}

The events will appear in the log file:

2016-10-18T11:14:11.0881912+10:00 0HKVMUG8EMJO9 [INF] Hello, world! (f83bcf75)

File format

By default, the file will be written in plain text. The fields in the log file are:

Field Description Format Example
Timestamp The time the event occurred. ISO-8601 with offset 2016-10-18T11:14:11.0881912+10:00
Request id Uniquely identifies all messages raised during a single web request. Alphanumeric 0HKVMUG8EMJO9
Level The log level assigned to the event. Three-character code in brackets [INF]
Message The log message associated with the event. Free text Hello, world!
Event id Identifies messages generated from the same format string/message template. 32-bit hexadecimal, in parentheses (f83bcf75)
Exception Exception associated with the event. Exception.ToString() format (not shown) System.DivideByZeroException: Attempt to divide by zero\r\n\ at...

To record events in newline-separated JSON instead, specify isJson: true when configuring the logger:

loggingBuilder.AddFile("Logs/myapp-{Date}.txt", isJson: true);

This will produce a log file with lines like:

{"@t":"2016-06-07T03:44:57.8532799Z","@m":"Hello, world!","@i":"f83bcf75","RequestId":"0HKVMUG8EMJO9"}

The JSON document includes all properties associated with the event, not just those present in the message. This makes JSON formatted logs a better choice for offline analysis in many cases.

Rolling

The filename provided to AddFile() should include the {Date} placeholder, which will be replaced with the date of the events contained in the file. Filenames use the yyyyMMdd date format so that files can be ordered using a lexicographic sort:

log-20160631.txt
log-20160701.txt
log-20160702.txt

To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point.

Message templates and event ids

The provider supports the templated log messages used by Microsoft.Extensions.Logging. By writing events with format strings or message templates, the provider can infer which messages came from the same logging statement.

This means that although the text of two messages may be different, their event id fields will match, as shown by the two "view" logging statements below:

2016-10-18T11:14:26.2544709+10:00 0HKVMUG8EMJO9 [INF] Running view at "/Views/Home/About.cshtml". (9707eebe)
2016-10-18T11:14:11.0881912+10:00 0HKVMUG8EMJO9 [INF] Hello, world! (f83bcf75)
2016-10-18T11:14:26.2544709+10:00 0HKVMUG8EMJO9 [INF] Running view at "/Views/Home/Index.cshtml". (9707eebe)

Each log message describing view rendering is tagged with (9707eebe), while the "hello" log message is given (f83bcf75). This makes it easy to search the log for messages describing the same kind of event.

Additional configuration

The AddFile() method exposes some basic options for controlling the connection and log volume.

Parameter Description Example value
pathFormat Filename to write. The filename may include {Date} to specify how the date portion of the filename is calculated. May include environment variables. Logs/log-{Date}.txt
minimumLevel The level below which events will be suppressed (the default is LogLevel.Information). LogLevel.Debug
levelOverrides A dictionary mapping logger name prefixes to minimum logging levels.
isJson If true, the log file will be written in JSON format. true
fileSizeLimitBytes The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, passnull. The default is 1 GiB. 1024 * 1024 * 1024
retainedFileCountLimit The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass null. The default is 31. 31
outputTemplate The template used for formatting plain text log output. The default is {Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception} {Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}

appsettings.json configuration

The file path and other settings can be read from JSON configuration if desired.

In appsettings.json add a "Logging" property:

{
  "Logging": {
    "PathFormat": "Logs/log-{Date}.txt",
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Information"
    }
  }
}

And then pass the configuration section to the AddFile() method:

loggingBuilder.AddFile(hostingContext.Configuration.GetSection("Logging"));

In addition to the properties shown above, the "Logging" configuration supports:

Property Description Example
Json If true, the log file will be written in JSON format. true
FileSizeLimitBytes The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, passnull. The default is 1 GiB. 1024 * 1024 * 1024
RetainedFileCountLimit The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass null. The default is 31. 31
OutputTemplate The template used for formatting plain text log output. The default is {Timestamp:o} {RequestId,13} [{Level:u3}] {Message} ({EventId:x8}){NewLine}{Exception} {Timestamp:o} {RequestId,13} [{Level:u3}] {Message} {Properties:j} ({EventId:x8}){NewLine}{Exception}

Using the full Serilog API

This package is opinionated, providing the most common/recommended options supported by Serilog. For more sophisticated configuration, using Serilog directly is recommended. See the instructions in Serilog.AspNetCore to get started.

The following packages are used to provide loggingBuilder.AddFile():

If you decide to switch to the full Serilog API and need help, please drop into the Gitter channel or post your question on Stack Overflow.

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. 
.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 (66)

Showing the top 5 NuGet packages that depend on Serilog.Extensions.Logging.File:

Package Downloads
Mongo.Migration

Package Description

Bellatrix.Trace

Bellatrix is a cross-platform, easily customizable and extendable .NET test automation framework that increases tests’ reliability.

Bellatrix.Logging

Bellatrix is a cross-platform, easily customizable and extendable .NET test automation framework that increases tests’ reliability.

bgTeam.Impl.Serilog

Package Description

UtilityFramework.Application.Core

Uilitarios para aplicações asp.net core

GitHub repositories (31)

Showing the top 5 popular GitHub repositories that depend on Serilog.Extensions.Logging.File:

Repository Stars
bitwarden/server
Bitwarden infrastructure/backend (API, database, Docker, etc).
dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
stryker-mutator/stryker-net
Mutation testing for .NET core and .NET framework!
belav/csharpier
CSharpier is an opinionated code formatter for c#.
Valkirie/HandheldCompanion
ControllerService
Version Downloads Last updated
9.0.0-dev-02302 97 11/27/2024
3.0.1-dev-00077 76,363 6/22/2022
3.0.0 4,498,204 6/22/2022
3.0.0-dev-00073 836 6/21/2022
3.0.0-dev-00067 27,843 3/21/2022
3.0.0-dev-00066 812 3/21/2022
3.0.0-dev-00065 8,519 1/19/2022
2.0.0 8,660,947 2/18/2020
2.0.0-dev-00039 107,829 12/11/2019
2.0.0-dev-00037 12,511 11/4/2019
2.0.0-dev-00032 318,111 4/2/2019
2.0.0-dev-00024 417,107 5/9/2018
2.0.0-dev-00023 246,395 9/7/2017
1.1.1-dev-00019 9,248 7/9/2017
1.1.1-dev-00017 1,768 7/6/2017
1.1.0 5,077,609 7/6/2017
1.1.0-dev-00014 1,701 6/29/2017
1.0.1 236,551 3/31/2017
1.0.1-dev-00008 6,825 12/29/2016
1.0.0 128,569 10/18/2016
1.0.0-dev-00004 1,574 10/18/2016
1.0.0-dev-00002 1,582 10/18/2016