NightOwl.Extensions.Logging.CustomJson 1.0.1

dotnet add package NightOwl.Extensions.Logging.CustomJson --version 1.0.1                
NuGet\Install-Package NightOwl.Extensions.Logging.CustomJson -Version 1.0.1                
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="NightOwl.Extensions.Logging.CustomJson" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NightOwl.Extensions.Logging.CustomJson --version 1.0.1                
#r "nuget: NightOwl.Extensions.Logging.CustomJson, 1.0.1"                
#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 NightOwl.Extensions.Logging.CustomJson as a Cake Addin
#addin nuget:?package=NightOwl.Extensions.Logging.CustomJson&version=1.0.1

// Install NightOwl.Extensions.Logging.CustomJson as a Cake Tool
#tool nuget:?package=NightOwl.Extensions.Logging.CustomJson&version=1.0.1                

NightOwl.Extensions.Logging.CustomJson

A customizable JSON formatter for .NET logging (Microsoft.Extensions.Logging) that allows you to control the structure and naming of your JSON log output.

Features

  • Same as JsonConsoleFormatter but with customizable field names
  • Customizable field names for all log properties
  • Custom log level name mapping

Installation

dotnet add package NightOwl.Extensions.Logging.CustomJson

Usage

Basic Setup

builder.Logging.AddConsoleFormatter<CustomJsonConsoleFormatter, CustomJsonConsoleFormatterOptions>();
builder.Services.AddLogging(builder =>
{
    builder.AddJsonConsole(options =>
    {
        options.FormatterName = "CustomJson";
        options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff";
        options.UseUtcTimestamp = true;
    });
});

Customizing Field Names

You can customize the names of fields in the JSON output:

builder.Logging.AddConsoleFormatter<CustomJsonConsoleFormatter, CustomJsonConsoleFormatterOptions>();
builder.Services.AddLogging(builder =>
{
    builder.AddJsonConsole(options =>
    {
        options.FormatterName = "CustomJson";
        options.TimestampFieldName = "time";
        options.LogLevelFieldName = "severity";
        options.MessageFieldName = "msg";
        options.CategoryFieldName = "logger";
        options.EventIdFieldName = "eventId";
        options.ExceptionFieldName = "error";
    });
});

Custom Log Level Names

You can map log levels to custom names:

builder.Logging.AddConsoleFormatter<CustomJsonConsoleFormatter, CustomJsonConsoleFormatterOptions>();
builder.Services.AddLogging(builder =>
{
    builder.AddJsonConsole(options =>
    {
        options.FormatterName = "CustomJson";
        options.LogLevelNameMapping = new Dictionary<LogLevel, string>
        {
            [LogLevel.Error] = "err",
            [LogLevel.Warning] = "warn",
            [LogLevel.Information] = "info",
            [LogLevel.Debug] = "dbug",
            [LogLevel.Trace] = "trc",
            [LogLevel.Critical] = "crit"
        };
    });
});

Configuration

You can also configure the formatter using appsettings.json:

Add the formatter to the logging configuration:

builder.Logging.AddConsoleFormatter<CustomJsonConsoleFormatter>();

Configure the formatter options in appsettings.json:

{
    "Logging": {
        "Console": {
            "FormatterName": "CustomJson",
            "FormatterOptions": {
                "LogLevelFieldName": "severity",
                "MessageFieldName": "msg",
                "CategoryFieldName": "logger",
                "EventIdFieldName": "eventId",
                "ExceptionFieldName": "error"
            }
        }
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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. 
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.1 45 1/28/2025