
Recently, my team was in charge of a legacy software written in .NET Framework. One of the biggest problems with this legacy is that we had no visibility of what was going on in.
One way to solve this problem was to send the software logs to Kibana (data visualization plugin for Elasticsearch). And it was the option we chose.
The output logs in “standard” format (to send to Kibana), make it difficult to create more complex graphs in Grafana (web application with interactive visualization of tables, graphs and alerts). Hence the need to generate logs in JSON format.
To generate logs in JSON format, we need to create a new layout and format the output in the desired format.
Our class will be named JsonLayout and must extend LayoutSkeleton (from the log4net.Layout namespace). The LayoutSkeleton class implements two interfaces (ILayout and IOptionHandler), so we must implement the methods: ActivateOptions and Format.
We already have our class created, we need to implement our custom formatting in the Format method.
In this example, we will print the fields in the log:
- messageObject (which we pass as a parameter when logging)
- requestId
- pid
- timestamp
- level
- logger
- location
- thread
- exceptionObject (exception that we pass as a parameter when logging an error)
- exceptionObjectString
This is how our class looks:
Now we just need to reference our JsonLayout class in the log4net.config file:
<configuration>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
...
<layout type="MyNamespace.JsonLayout">
</layout>
</appender>
...
</log4net>
</configuration>
We write a log in our application:
And voilà:
{
"messageObject": {
"message": "new log message",
"field": "some data"
},
"requestId": "9e20b1b3-9064-4ebf-9d84-6662c7673324",
"pid": 25829,
"timestamp": "2020-12-17T00:16:15.0696380Z",
"level": "INFO",
"logger": "Log4netOutputJson.Program",
"location": "Log4netOutputJson.Program",
"thread": "1",
"exceptionObject": null,
"exceptionObjectString": null
}
The sample project is hosted on GitHub: https://github.com/victorlss/log4net-output-json