JetBlack.JsonConsoleLogger 6.0.0

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

// Install JetBlack.JsonConsoleLogger as a Cake Tool
#tool nuget:?package=JetBlack.JsonConsoleLogger&version=6.0.0

JetBlack.JsonConsolerLogger

Overview

This library provides a JSON console logger for the Microsoft.Extensions.Logging framework.

It was originally written for .Net services running in docker to provide structured logging for use with Elasticsearch.

Issues

At present the public interface provided by the Microsoft logging framework passes an important parameter as an internal class. Reflection is used in order to access this parameters. If the underlying implementation is changed this logger will break.

Installation

The package can be installed from nuget.

Usage

This is a modification of the logger in Microsoft.Extensions.Logging.Console, and can be used in exactly the same way. Please look at the Microsoft documentation for more examples.

Here is an example using a logger factory.

using JetBlack.JsonConsoleLogger;

namespace Example
{
    class Program
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .Build();

        var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder
                .AddConfiguration(configuration.GetSection("Logging"))
                .AddJsonConsole();
        });

        var logger = loggerFactory.CreateLogger<Program>();

        logger.LogInformation("This is an {LevelName} message with a {Date}", "INFO", DateTime.Now);

        loggerFactory.Dispose();
    }
}

Here is a basic appsettings.json file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    },
    "JsonConsoleLogger": {
      "Timestamp": "utc"
    }
  }
}

This would produce the following output.

{"name":"Example.Program","level":"information","message":"This is an INFO message with a 09/30/2019 14:42:48","parameters":{"LevelName":"INFO","Date":"2019-09-30T14:42:48.2281211+01:00"},"timestamp":"2019-09-30T13:42:48.2402817"}

Each message is newline terminated.

Configuration

Timestamps

The Timestamp option can be one of: none, local, utc.

Scopes

To include scope information set IncludeScopes to true.

Logging to standard error

If LogToStdErr is true the output will be sent to stderr, otherwise it will go to stdout.

Disabling exception logging

If LogExceptions is set to false the exception messages will not be output.

Flattening exceptions

By default inner exceptions will be output as a nested dictionary. Setting flattenExceptions to true changes this to a list with the outermost at the start of the list and the innermost at the end.

Tag names

Tags names can be overridden with a names dictionary.

Here is an example:

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    },
    "JsonConsole":
    {
      "Timestamp": "utc",
      "logToStdErr": true,
      "names": {
        "name": "logName",
        "level": "logLevel",
        "message": "logMessage",
        "parameters": "logParameters",
        "exception": "logException",
        "timestamp": "logTimestamp",
        "exceptionMessage": "logExceptionMessage",
        "innerException": "logInnerException",
        "stackTrace": "logStackTrace",
        "lineNumber": "logLineNumber",
        "columnNumber": "logColumnNumber",
        "fileName": "logFileName",
        "method": "logMethod",
        "trace": "trce",
        "debug": "dbug",
        "information": "info",
        "Warning": "warn",
        "error": "fail", 
        "critical": "crit"
      }
    }
  }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
6.0.0 289 1/5/2022
1.0.0 489 9/30/2019