For that task, Serilog 2.0 provides JsonValueFormatter: Writing out property data is harder – but much more consistent.
The amount of code involved is so small that, with the help of a JSON string escaping function, its cleaner and faster for implementers to hand-code than it is for Serilog to provide an infinitely-flexible generic formatter. Writing out the fixed properties is easy, but varies a lot between JSON formats. There are two main tasks involved, both of which were previously the responsibility of JsonFormatter:įormatting the fixed properties of LogEvent such as Timestamp, Level and the message, andįormatting the associated property data encoded in LogEventPropertyValue objects. Sinks that use JSON need to take a Serilog LogEvent and write it out to a stream of text. In this post we’ll look at some new formatting infrastructure, a new compact JSON representation, and finally some API improvements that make it easier to use JSON with Serilog across the board. If you use JSON logging with Serilog, or maintain a Serilog sink that writes JSON, Serilog 2.0 has some improvements in these areas. Ubiquity - practically everywhere that Serilog supports plain text, JSON support is needed too, but not being comfortable with the format or implementation we’ve held back in a number of places This left us with several longstanding pain points:Įfficiency - the default format written by JsonFormatter is a straightforward mapping from Serilog’s LogEvent type, which requires a lot more space than is strictly necessary (even for JSON!)įlexibility - as formatting requirements expanded, JsonFormatter did too, eventually becoming a big ball of virtually-dispatched mud, that’s unable to evolve without breaking subclasses Serilog has always had good support for writing JSON logs, but when JsonFormatter was introduced in Serilog 1.0 I underestimated its importance, and underestimated the breadth of scenarios in which it would be used. Practically every modern logging target of any significance requires, supports, or uses JSON in one way or another. JSON turns out to be a good fit for storing and transporting structured log events. Regarding upgrades by the way: if you get stuck, please post to Stack Overflow and tag your question with serilog – many Serilog users and contributors are there ready to help out, and by posting there you’ll help others find the information they need.
Go go go! :-)īuilding the new release was a team effort, and announcing it will be as well, so instead of trying to cover everything we’ve added since the 1.x series I’m going to write about a few new features I am especially interested in, and call out posts from the rest of the team as they appear.
Serilog 2.0 is out! There’s an Upgrade Guide and Release Notes already posted on the Serilog site, and a good number of first- and third-party packages have already been updated to work with the new version.