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
<PackageReference Include="NightOwl.Extensions.Logging.CustomJson" Version="1.0.1" />
paket add NightOwl.Extensions.Logging.CustomJson --version 1.0.1
#r "nuget: NightOwl.Extensions.Logging.CustomJson, 1.0.1"
// 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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Logging.Console (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
- System.Text.Json (>= 8.0.5)
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 |