Home

Awesome

:mega: Important notices
* If you're upgrading from 2.x to 3.x, there's several breaking changes to be aware of. See the notes below for the details.<br /> * Documentation of the previous major version (2.x) is available here.

Karambolo.Extensions.Logging.File

This class library contains a lightweight implementation of the Microsoft.Extensions.Logging.ILoggerProvider interface for file logging. Runs on all .NET platforms which implement .NET Standard 2.0+ including .NET Core 2 (ASP.NET Core 2.1+), .NET Core 3 and .NET 5+.

NuGet Release Donate

The code is based on ConsoleLogger whose full feature set is implemented (including log scopes and configuration reloading). The library has no 3rd party dependencies. No I/O blocking occurs as processing of log messages is done in the background. File system access is implemented on top of the Microsoft.Extensions.FileProviders.IFileProvider abstraction so it's even possible to use a custom backing storage.

As of version 3.3.0 JSON structured logging (following the format established by the JSON formatter of the built-in console logger) is also available.

As of version 3.6.0 the self-contained trimmed and Native AOT deployments models are also supported (in applications running on .NET 8 or newer).

Additional features:

Important notes for existing consumers

Version 3.0 is a major revision with many improvements involving some breaking changes:

Thus, version 3.0 is not backward compatible with previous versions. If you want to upgrade from older versions, please read up on the new configuration system to be able to make the necessary adjustments.

However, you may stay with version 2.1 as it continues to work on .NET Core 3+ according to my tests (but please note that it isn't developed actively any more).

Installation

Add the Karambolo.Extensions.Logging.File NuGet package to your application project:

dotnet add package Karambolo.Extensions.Logging.File

or, if you want structured logging, add Karambolo.Extensions.Logging.File.Json NuGet package instead:

dotnet add package Karambolo.Extensions.Logging.File.Json

If you have a .NET Core/.NET 5+ project other than an ASP.NET Core web application (e.g. a console application), you should also consider adding explicit references to the following NuGet packages with the version matching your .NET runtime. For example, if your project targets .NET 8:

dotnet add package Microsoft.Extensions.FileProviders.Physical -v 8.0.*
dotnet add package Microsoft.Extensions.Logging.Configuration -v 8.0.*
dotnet add package Microsoft.Extensions.Options.ConfigurationExtensions -v 8.0.*
<details> <summary>Explanation why this is recommended</summary>

The Karambolo.Extensions.Logging.File package depends on some framework libraries and references the lowest possible versions of these dependencies (e.g. the build targeting .NET 8 references Microsoft.Extensions.Logging.Configuration 8.0.0). These versions may not (mostly do not) align with the version of your application's target platform since that may be a newer patch, minor or even major version (e.g. .NET 9). Thus, referencing Karambolo.Extensions.Logging.File in itself may result in referencing outdated framework libraries on that particular platform (sticking to the previous example, Microsoft.Extensions.Logging.Configuration 8.0.0 instead of 9.0.0).

Luckily, in the case of ASP.NET Core this is resolved automatically as ASP.NET Core projects already reference the correct (newer) versions of the framework libraries in question (by means of the Microsoft.AspNetCore.App metapackage).

However, in other cases (like a plain .NET Core/.NET 5+ console application) you may end up with outdated dependencies, which is usually undesired (even can lead to issues like this), so you want to resolve this situation by adding the explicit package references listed above.

For more details, see NuGet package dependency resolution.

</details>

Configuration

If you've chosen structured logging, replace calls to AddFile(...) with AddJsonFile(...) in the following samples.

<details> <summary>A sidenote regarding structured logging</summary>

AddJsonFile is just a convenience method which sets the TextBuilder setting to the default JSON formatter (JsonFileLogEntryTextBuilder) globally. You can achieve the same effect by using AddFile and setting TextBuilder (or TextBuilderType) to the aforementioned formatter manually. For details, see the Settings section.

It also follows from the above that you can still override this setting in your configuration (appsettings.json or configure callback) and use other formatters regardless the defaults set by AddJsonFile.

</details>

.NET Core 3, .NET 5+

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddFile(o => o.RootPath = builder.Environment.ContentRootPath);

var app = builder.Build();

// ...
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .ConfigureLogging((ctx, builder) =>
                    {
                        builder.AddConfiguration(ctx.Configuration.GetSection("Logging"));
                        builder.AddFile(o => o.RootPath = ctx.HostingEnvironment.ContentRootPath);
                    })
                    .UseStartup<Startup>();
            });
}
// build configuration
var configuration = /* ... */;

// configure DI
var services = new ServiceCollection();

services.AddLogging(builder =>
{
    builder.AddConfiguration(configuration.GetSection("Logging"));
    builder.AddFile(o => o.RootPath = AppContext.BaseDirectory);
});

// create logger factory
await using (var sp = services.BuildServiceProvider())
{
    var loggerFactory = sp.GetService<ILoggerFactory>();
    // ...
}

.NET Core 2

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((ctx, builder) =>
            {
                builder.AddConfiguration(ctx.Configuration.GetSection("Logging"));
                builder.AddFile(o => o.RootPath = ctx.HostingEnvironment.ContentRootPath);
            })
            .UseStartup<Startup>();
}
// build configuration
var configuration = /* ... */;

// configure DI
var services = new ServiceCollection();

services.AddLogging(builder =>
{
    builder.AddConfiguration(configuration.GetSection("Logging"));
    builder.AddFile(o => o.RootPath = AppContext.BaseDirectory);
});

// create logger factory
using (var sp = services.BuildServiceProvider())
{
    var loggerFactory = sp.GetService<ILoggerFactory>();
    // ...
}

Advanced use cases

Using multiple providers with different settings

First of all, you need a little bit of boilerplate code:

[ProviderAlias("File2")]
class AltFileLoggerProvider : FileLoggerProvider
{
    public AltFileLoggerProvider(FileLoggerContext context, IOptionsMonitor<FileLoggerOptions> options, string optionsName) : base(context, options, optionsName) { }
}

And a setup like this:

services.AddLogging(builder =>
{
    builder.AddConfiguration(config.GetSection("Logging"));
    builder.AddFile(o => o.RootPath = AppContext.BaseDirectory);
    builder.AddFile<AltFileLoggerProvider>(configure: o => o.RootPath = AppContext.BaseDirectory);
});

Now, you have two independent file logger providers. One of them picks up its configuration from the standard configuration section "File" while the other one from section "File2" as specified by the ProviderAlias attribute.

You may check out this demo application which shows a complete example of this advanced setup.

Customizing/extending the logging logic

The implementation of the file logger provides many extension points (mostly, in the form of overridable virtual methods), so you can customize its behavior and/or implement features that are not available out of the box.

For example, see this sample application, which extends the file logger with the ability to rotate log files.

Settings

Provider settings

There are some settings which are configured on provider level only (FileLoggerOptions):

DescriptionDefault valueNotes
FileAppenderSpecifies the object responsible for appending log messages.PhysicalFileAppender instance with root path set to Environment.CurrentDirectoryThe RootPath shortcut property is also available for setting a PhysicalFileAppender with a custom root path. (This path must point to an existing directory.)
BasePathPath to the base directory of log files."" (none)Base path is relative to (but cannot point outside of) the root path of the underlying file provider (FileAppender.FileProvider). (If this path does not exist, it will be created automatically.)
FilesAn array of LogFileOptions which define the settings of the individual log files.There is an important change compared to older (2.x or earlier) versions: you must explicitly define at least one log file, otherwise the provider won't log anything.

Log file settings

These settings can be configured on log file level only (LogFileOptions):

DescriptionDefault valueNotes
PathPath of the log file relative to FileLoggerOptions.BasePath.Can be a simple path or a path template.<br/>Templates specify placeholders for date and counter strings like "<date>/app-<counter>.log".<br/>The file definition is ignored if Path is null or empty.<br/>Path should be unique, multiple definitions with the same Path value may lead to erroneous behavior.
MinLevelDefines log level switches for the individual file.Works similarly to LogLevel filter switches. However, this is a second-level filter which can only tighten the rules of the first-level LogLevel filters (as it can only filter messages that hit the logger provider).

Globally configurable log file settings

The log file settings below can be specified globally (per provider) and individually (per log file) as well. File-level settings always override provider-level settings. (In practice this means if a setting property is null for a given file, the value of the same property of the provider-level settings applies. If that is null too, a default value is used.)

DescriptionDefault valueNotes
FileAccessModeStrategy for accessing log files.LogFileAccessMode. KeepOpenAndAutoFlush<ul><li>KeepOpenAndAutoFlush: Keeps open the log file until completion and flushes each entry into the file immediately.</li><li>KeepOpen: Keeps open the log file until completion but entries are flushed only when internal buffer gets full. (Provides the best performance but entries don't appear instantly in the log file.)</li><li>OpenTemporarily: Opens the log file only when an entry needs to be written and then closes it immediately. (Provides the worst performance but log files won't be locked by the process.)</li></ul>
FileEncodingCharacter encoding to use.Encoding.UTF8The FileEncodingName shortcut property is also available for setting this option using an encoding name.
DateFormatSpecifies the default date format to use in log file path templates."yyyyMMdd"Value must be a standard .NET format string which can be passed to DateTimeOffset.ToString.<br/>Date format can even be specified inline in the path template: e.g. "<date:yyyy>/app.log"
CounterFormatSpecifies the default counter format to use in log file path templates.basic integer to string conversionValue must be a standard .NET format string which can be passed to Int32.ToString.<br/>Counter format can even be specified inline in the path template: e.g. "app-<counter:000>.log"
MaxFileSizeIf set, new files will be created when file size limit is reached.Path must be a template containing a counter placeholder, otherwise the file size limit is not enforced.
TextBuilderSpecifies a custom log text formatter.FileLogEntryTextBuilder. InstanceFor best performance, if you set this to a formatter of the same type for multiple files, use the same formatter instance if possible.<br/>The TextBuilderType shortcut property is also available for setting this option using a type name.<br/>For an example of usage, see this sample application.
IncludeScopesEnables including log scopes in the output.falseWorks exactly as in the case of ConsoleLogger.
MaxQueueSizeDefines the maximum capacity of the log processor queue (per file).0 (unbounded)If set to a value greater than 0, log entries will be discarded when the queue is full, that is, when the specified limit is exceeded.
PathPlaceholderResolverProvides a way to hook into path template resolution.Callback which can be used to customize or extend the resolution of path template placeholders. Enables special formatting, custom placeholders, etc.<br/>For an example of usage, see this sample application.<br/>(Available only since version 3.2.0)

Sample JSON configuration

{
  "Logging": {
    // global filter settings
    "LogLevel": {
        "Default": "Information"
    },
    // provider level settings
    "File": {
      "BasePath": "Logs",
      "FileAccessMode": "KeepOpenAndAutoFlush",
      "FileEncodingName": "utf-8",
      "DateFormat": "yyyyMMdd",
      "CounterFormat": "000",
      "MaxFileSize": 10485760,
      "TextBuilderType": "MyApp.CustomLogEntryTextBuilder, MyApp",
      // first-level filters
      "LogLevel": {
        "MyApp": "Information",
        "Default": "Debug" // first-level filters can loosen the levels specified by the global filters
      },
      "IncludeScopes": true,
      "MaxQueueSize": 100,
      "Files": [
        // a simple log file definition which inherits all settings from the provider (will produce files like "default-000.log")
        {
          "Path": "default-<counter>.log"
        },
        // another log file definition which defines extra filters and overrides the Counter property (will produce files like "2019/08/other-00.log")
        {
          "Path": "<date:yyyy>/<date:MM>/other-<counter>.log",
          // second-level filters
          "MinLevel": {
            "MyApp.SomeClass": "Warning",
            "Default": "Trace" // this has no effect as second-level filters can only be more restrictive than first-level filters!
          },
          "CounterFormat": "00"
        }
      ]
    }
  }
}

Troubleshooting

If you have added the right NuGet package and configured logging in your application by the above but the application outputs no log files, check the following points:

If none of these helps, since version 3.2.0 you can track down the problem by observing the file logger's diagnostic events:

// this subscription should happen before anything is logged,
// so place it in your code early enough (preferably, before configuration of logging)
FileLoggerContext.Default.DiagnosticEvent += e =>
{
    // examine the diagnostic event here:
    // print it to the debug window, set a breakpoint and inspect internal state on break, etc.
    Debug.WriteLine(e);
};