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:
An audit trail (also called audit log) is one of the most effective ways to track user actions, which provides evidence of a sequence of activities that affect information, processes, and so on.
Using Entity Framework Core, we can easily audit changes overriding SaveChanges() method.
Let get it started
Define a context and entity classes that make up your model:
So, we should define an auditable context and AuditTrail model:
Execute EF migrations commands to create database:
dotnet ef migrations add InitialCreate --context BloggingContext
dotnet ef database update --context BloggingContext
Let’s test it
Add data on database:
INSERT INTO [Blogging].[dbo].[Blogs] (Url)
VALUES ('blog.victorleonardo.com');