Introduction
In the current Software Development phase, logging is an integral part. The development process has been changed and in today's scenario, there are different API calls. So, structured logging is essential for debugging, troubleshooting, or logging any other information. Serilog is useful for structured logging and therefore, the main purpose of using Serilog is to log the Structured Data.
Example for Logging
Have a look at the examples shown below.
Where {val} is called a Named Property and Math.PI is the value of the named property.
Sinks
Sinks are the places where we store our log messages. For our demo application, we will use the sinks like a Console or File. There are many other sinks available that allow storing data in a structured format like Ravan DB/Seq.
Here's a list of some commonly used sinks.
- Azure Table Storage
- Console
- File/Rolling file
- Log4Net
- Logger
- MongoDB
- Microsoft SQL Server
- Raven DB
- Seq
Logging Structured Data
- Collection
- Objects
- Destructured Object
We will now create a sample application for Serilog.
![Serilog]()
Next, install the Nugget package for Serilog as shown below.
![Nugget package]()
![Sample]()
Code Sample for creating a console sink
Now, have a look at the preceding example. Commented lines are for writing the logging-in and rolling files.
From the following lines of code, you can understand how to assign the logger.
![Output]()
Formatting
There are many formatting options available for Serilog.
The default Serilog shows a string in “”, but with the option {username:l}, we can remove “.
Here's an example of numbers formatting.
Logging Levels
- Verbose: Very low-level debugging
- Debug: low-level control logic
- Information
- Warning
- Error
- Fatal
By default, the logging level is set to Information. We can set the minimum logging level in the code.
Adding the Log Context
I have introduced some basic understanding of Serilog in this article. There will be an introduction to some advanced features of Serilog and the Seq (Sinks) in my next article.