Jsonconsole in asp.net core 5.0


Jsonconsole in asp.net core 5.0


New log in asp.net core 5.0JsonConsole, or output log toConsole, but it will be appliedJsonOne of the formatsFormatterFormat the log into JSON and output it to the console


Let’s take a look at an example. Take a small project of mine as an example to demonstrate that the default console log format is multi-line and not structured enough. If you want to process it, it will be more troublesome and can’t be easily processed and extended. JSON is a relatively more structured data, and it’s relatively convenient to expand it.

Let’s take a look at the default log output effect:

default console log

As shown in the figure, a log has two lines by default. The first line is the corresponding line between the log level and the logCategoryNameandEventId, the second line is the specific content of the log

Then let’s use itJsonConsoleThe configuration method is as follows:Program.csCall (recommended)

json console configure

Let’s look at the output log:

json console log

You can see that the current log is JSON, and each log is a JSON


Its implementation lies in aJsonConsoleFormatter, based onSystem.Text.JsonTo achieveJsonformat,

We can also achieve our own goalsConsoleFormatterFrom definitionConsoleFor detailed implementation of the log format, please refer to:https://github.com/dotnet/runtime/blob/v5.0.0/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs

Extension method implementationhttps://github.com/dotnet/runtime/blob/v5.0.0/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerExtensions.cs

AddJsonConsoleExtended implementation:







In previous versions, if you want to format the log into JSON and output it to the console, you need to use a third-party log framework to serialize the log into JSONJsonConsoleFormatterAfter that, we can easily format the log into JSON and output it to the console.

After the log is output as JSON, it is easy to expand and supplement the log. NETCORE 3.1, which is currently used by our application, uses NLog to format the log into JSON and output it to the console, and then collects es through fluent. At the same time, the information of the k8s environment where the application is located, such as containername, podname, clustername, etc., will also be patched into the log information

The example uses the default configuration. We can also customize the configuration formatted by JSON through the overload extension method with action


Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]