asemin.FileLogger 1.0.2

dotnet add package asemin.FileLogger --version 1.0.2
                    
NuGet\Install-Package asemin.FileLogger -Version 1.0.2
                    
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="asemin.FileLogger" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="asemin.FileLogger" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="asemin.FileLogger" />
                    
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 asemin.FileLogger --version 1.0.2
                    
#r "nuget: asemin.FileLogger, 1.0.2"
                    
#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 asemin.FileLogger@1.0.2
                    
#: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=asemin.FileLogger&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=asemin.FileLogger&version=1.0.2
                    
Install as a Cake Tool

FileLogger

Microsoft.Extensions.Logging file logger for .NET / ASP.NET Core / .NET Worker.

Why this one?

log file appender friendly. That means it does always write in the same (first) log file and on file roll over it moves the content into a new file. So you can use tools like Tailblazer or Advanced Log Viewer

Getting started

Install nuget package https://www.nuget.org/packages/asemin.FileLogger/

dotnet add package asemin.FileLogger

Either use application builder

using asemin.FileLogger;

var builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddFile();

or use default builder

using asemin.FileLogger;

var builder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args);

builder.ConfigureLogging(x => x.AddFile());

Tip: If you do not add any configuration, a default one will be used:

  • Log file: /logs/<appname>.app.log and appname is the IHostEnvironment.ApplicationName
  • max file size: 10 MiB
  • max files: 10
  • Default log level: Trace (Remember, the global log filters apply before this one)

Configuration

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "File": {
      "Files": {
        "default": {
          "File": "logs/logs.log",
          "MaxSize": 104857600,
          "MaxFiles": 100,
          "LogLevel": {
            "Default": "None",
            "MyClass.Logger": "Debug"
          }
        },
        "other": {
          "File": "%ProgramData%/My app/otherLogFile.log",
          "MaxSize": 10485760,
          "MaxFiles": 10,
          "LogLevel": {
            "MyOtherClass": "Trace",
            "Microsoft": "Debug"
          }
        }
      }
    }
  }
}

In Logging.File.Files you can add as many files loggers as you want. The names (default, other) are only used by .NET configuration. So you can make proper overrides with debug or production settings.

Property Default Description
File logs/logs.log Name of the log file. Supports relative paths, absolut paths and environment parameters expansion (e.g. %appdata%).
MaxSize 10485760 (10 MiB) Max size of log file in bytes. If file exceeds this size a roll over will happen.
MaxFiles 10 Max number of log files. Older files get deleted first.
LogLevel Trace Works the same way as Microsoft global LogLevel. Warning: this one applies AFTER global one. So you cannot set the level for a message lower than the global one.

C# code

builder.Logging.AddFile(x =>
{
    x.Files = new Dictionary<string, LoggerConfig>
    {
        {
            "default",
            new LoggerConfig
            {
                File = @"C:\temp\my app\logs\logs.log",
                MaxSize = 100 * 1024 * 1024,
                MaxFiles = 25
            }
        }
    };
});

F# code

builder.Logging.AddFile(fun config ->
    config.Files <-
        dict[("default", LoggerConfig(
            File = "mylog.log",
            MaxSize = 10 * 1024 * 1024,
            MaxFiles = 17))])

TODOs

  • try out in serious applications (confirmed)
  • add support for different filters for each log file
  • Test with Linux (confirmed)
  • customize format of logged messages (date, thread, level, etc.) (does someone actually uses that?)
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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. 
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
1.0.2 120 4/12/2025
1.0.0 278 5/18/2024
0.9.0 160 4/17/2024
0.8.0 177 2/19/2024
0.7.0 156 2/19/2024
0.6.0 151 2/18/2024
0.4.0 142 2/17/2024
0.3.0 144 2/17/2024
0.2.0 142 2/17/2024
0.1.0 137 2/17/2024