Nefarius.Utilities.AspNetCore 2.4.0-pre001

Prefix Reserved
This is a prerelease version of Nefarius.Utilities.AspNetCore.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Nefarius.Utilities.AspNetCore --version 2.4.0-pre001                
NuGet\Install-Package Nefarius.Utilities.AspNetCore -Version 2.4.0-pre001                
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="Nefarius.Utilities.AspNetCore" Version="2.4.0-pre001" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nefarius.Utilities.AspNetCore --version 2.4.0-pre001                
#r "nuget: Nefarius.Utilities.AspNetCore, 2.4.0-pre001"                
#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 Nefarius.Utilities.AspNetCore as a Cake Addin
#addin nuget:?package=Nefarius.Utilities.AspNetCore&version=2.4.0-pre001&prerelease

// Install Nefarius.Utilities.AspNetCore as a Cake Tool
#tool nuget:?package=Nefarius.Utilities.AspNetCore&version=2.4.0-pre001&prerelease                

Nefarius.Utilities.AspNetCore

GitHub Workflow Status Requirements Nuget Nuget

My opinionated collection of utilities for ASP.NET Core applications.

Features

  • Sets up application (and optionally web requests) logging with Serilog
  • Sets up application log file rotation and compression
  • Sets up W3C logging to a separate access log file
    • Compresses rolled W3C log files and allows for their own retention settings
  • Sets up forwarded headers and auto-configures local networks so the correct client IP ends up in logs and middleware
    • ⚠️ This assumes that your app sits behind a reverse proxy, do not enable this setting if your app faces the Internet directly or header spoofing becomes possible!
  • ... and more as I start incorporating this lib in my projects!

How to use

Replace 👇

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

with 👇

WebApplicationBuilder builder = WebApplication.CreateBuilder(args).Setup();

and 👇

WebApplication app = builder.Build();

with 👇

WebApplication app = builder.Build().Setup();

and you're all set! 👏 The Setup extension methods take optional configuration arguments you can provide to alter the default behaviour.

Loading additional configuration

Let's assume you have a custom /app/secrets/appsettings.json file adding one or more additional sinks (MongoDB in this example):

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.MongoDB"
    ],
    "WriteTo": [
      {
        "Name": "MongoDBBson",
        "Args": {
          "databaseUrl": "mongodb+srv://db-cluster/database?authSource=admin",
          "collectionName": "logs",
          "cappedMaxSizeMb": "1024",
          "cappedMaxDocuments": "50000",
          "rollingInterval": "Month"
        }
      }
    ]
  }
}

To have this configuration file read/merged you can access and modify the Configuration of the WebApplicationBuilderOptions like so:

WebApplicationBuilder builder = WebApplication.CreateBuilder(args).Setup(opts =>
{
    // loads settings from K8s secret
    opts.Configuration.AddJsonFile("secrets/appsettings.json", optional: true);
});

This ensures that the Serilog sinks configuration is read early enough when the logger is created.

Example configurations

From code

Enable and customize W3C log compression

The following settings use the library defaults, they're simply explained here and don't need to be exclusively set if you're satisfied with the defaults 😉

var builder = WebApplication.CreateBuilder().Setup(options =>
{
    // this will only keep three most recent uncompressed log files
    options.W3C.RetainedFileCountLimit = 3;
    // on rotation, make a compressed archive copy before deleting the original
    options.W3C.CompressDeletedLogFiles = true;
    // keeps the last 90 compressed log files on top of the original files
    // after this, even the compressed logs are finally deleted from disk
    options.W3C.RetainedCompressedFileCountLimit = 90;
});

From appsettings.json

You can also alter the defaults from your configuration; simply stick to the options classes and property naming conventions like so:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "WebApplicationBuilderOptions": {
    "AutoDetectPrivateNetworks": false
  },
  "WebApplicationOptions": {
    "UseForwardedHeaders": false
  }
}

Bear in mind that changing the same option in code will take priority over application configuration.

From docker-compose.yml

Using this format you can change the settings directly in the compose file:

...
    environment:
      - TZ=Europe/Vienna
      - WebApplicationBuilderOptions__W3C__RetainedCompressedFileCountLimit=600
      - WebApplicationBuilderOptions__W3C__RetainedFileCountLimit=12
...

3rd party credits

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 is compatible.  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. 
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
2.5.1 124 12/14/2024
2.5.0 426 11/13/2024
2.4.0 194 11/8/2024
2.4.0-pre001 73 11/8/2024
2.3.1 129 10/29/2024
2.3.0 2,038 3/8/2024
2.2.3 123 3/6/2024
2.2.2 239 2/25/2024
2.2.1 243 2/19/2024
2.2.0 430 1/13/2024
2.1.0 95 1/13/2024
2.0.0 101 1/11/2024
1.16.0 362 12/7/2023
1.15.0 266 11/19/2023
1.14.0 138 11/17/2023
1.13.0 154 11/17/2023 1.13.0 is deprecated because it has critical bugs.
1.12.0 119 11/17/2023 1.12.0 is deprecated because it has critical bugs.
1.11.0 432 9/16/2023
1.10.1 148 9/13/2023
1.10.0 151 9/13/2023
1.9.1 155 9/12/2023
1.8.4 331 8/7/2023
1.8.3 640 4/30/2023
1.8.0 186 4/30/2023
1.7.3-pre 142 4/30/2023
1.7.2-pre 136 4/30/2023
1.7.1 174 4/29/2023
1.7.0 194 4/29/2023
1.6.1 198 4/28/2023
1.6.0 189 4/27/2023
1.5.1 181 4/27/2023
1.5.0 193 4/27/2023
1.4.2 171 4/27/2023
1.4.1 592 1/24/2023
1.4.0 352 1/5/2023
1.3.0 317 1/4/2023
1.0.1 299 1/4/2023
1.0.0-pre 164 1/4/2023