adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
147 stars 22 forks source link

JSON Format differences to JsonConsoleLogger #26

Closed markusschaber closed 1 year ago

markusschaber commented 1 year ago

Hi, I'm evaluating Karambolo.Extensions.Logging.File.Json and the README states:

The code is based on ConsoleLogger whose full feature set is implemented

But while experimenting, I found some small differences to AddJsonConsole().

First, it looks like Karambolo separates entries with a ',', while the console logger only uses line break.

Second, the JsonConsoleFormatterOptions accept JsonWriterOptions, where we can set 'Indented = false'.

This combination allows us to write the JSON lines (aka NDJSON) format, each log entry on a single line, with the console logger.

Would you implement the changes, or accept patches, to enable JSON lines support?

adams85 commented 1 year ago

Hi @markusschaber,

You can pass JsonWriterOptions to the logger using this configuration overload as follows:

builder.Logging.AddJsonFile(textBuilder: new JsonFileLogEntryTextBuilder(jsonWriterOptions), configure: o => { /* ... */ });

For further customization, you can subclass JsonFileLogEntryTextBuilder and pass an instance of that to the method above. For example, to remove the trailing comma, you'll need to override the BuildEntryText method.

Would you implement the changes, or accept patches, to enable JSON lines support?

Improvements are welcome!

adams85 commented 1 year ago

FYI: in the latest version (v3.5.0), I added some convenience methods along with an option class so you don't need to subclass JsonFileLogEntryTextBuilder any more if you just want to change the separator between entries:

builder.Logging.AddJsonFile(new JsonFileLogFormatOptions { JsonWriterOptions = new JsonWriterOptions { Indented = false }, EntrySeparator = "" }