Using Serilog For Logging Operations on .Net

Server Tombak
nacressoftware
Published in
5 min readApr 30, 2024

Why do we use Logging and how does it help us?

In real life projects, problems can occur like not adding, deleting a data or having a problem about the connection which may effect the system’s regular performance. So how do we know about it?

That’s one of the cases where we are using logging when there are a problem developer generally want to know about it. So thats the way that they can handle it to stop having the same problem again. And we can make that with logging. We can make the system write somewhere when there is an error with the error message or our own configuration. And even we can write information messages to know about that the system running successfully, without having any problem. We do that by logging that informations, errors warnings to a file, console, database etc. And then we can control it to learn how the system works, is there any problem if there is what may be the reason to having it. That’s the some of reason why we use logging in our projects

Serilog is a .Net logging package. Its easy to use, has a clean api and runs on all .NET platforms. So with serilog we can manage logging in our projects and it has various log types like; Debug, Error, Warning, Information, Criticals and Trace and we can write them to console, debug and file. And one of the other advantage of using serilog for logging is that it can work as asynchronously too.

Entegration Serilog on a project — The Coding Part :)

First I will start from creating a Web Api project and then we will add needed packages;

  1. Create an Api project
  2. Add “Serilog.AspNetCore” Package from Nuget Packages

So if you created the project and added the needed package let’s go on.

Now we should make the configuration for Serilog, we need to add the following code snippet to the program.cs file;


builder.Services.AddSerilog();

Log.Logger = new LoggerConfiguration()
.WriteTo.Debug(Serilog.Events.LogEventLevel.Information)
.WriteTo.Console()
.WriteTo.File("logs.txt")
.CreateLogger();

In the above code snippet I added WriteTo.Debug(), file and Console and you can do it as your needs. And the following method is for choosing log level “Serilog.Events.LogEventLevel.Information”

These are Log Level that we can use;

LogLevel

When we choose the information as the log level we will not be able to see Trace and debug logs. So you can decide your Logging level with Serilog.Events.LogEventLevel.Information” method. Check the Log Levels from above image which is log level enum and set it on program.cs file.

Cleaning Default Logging settings on appsettings.json

After completing the configuration part for serilog on program.cs file we should not forget to clean your Appsetting.json file’s Logging information.

Appsetting.json

As you can see the above image, that comes defaultly when we create the project. But since we handle logging ourself we should delete the old configuration on appsettings.json. Don’t forget to check if there is any other json files. If there is you should clean logging information there too.

Calling Logging methods and visualizing them

We have completed the configuratiın now we can call our serilog methods.

I have created a demo controller api where we will write and call logging methods:

 [Route("api/[controller]")]
[ApiController]
public class DemoController : ControllerBase
{
private readonly ILogger<DemoController> _logger;
public DemoController(ILogger<DemoController> logger)
{
_logger = logger;
}

[HttpGet]
public IActionResult Get()
{
_logger.LogInformation("This is an information Log");
_logger.LogError("This is an Error Log");
_logger.LogWarning("This is a Warning Log");

return Ok();
}
}

I have added three logging level logs to the Get method and its time to visualize them. Since I have created the project as Api project swagger added defaultly and I will call DemoController Get method there:

Serilog api call

If we execute it, that’s what we will see on our console:

Console Serilog Visualization

As you can see from the above image of our console: our logging methods that we have written on DemoController Get method run successfully and we can see our messages on console.

And of course, we can save them on a file, as we already did. If you remember in the configuration part we have added the WriteTo.File(“logs.txt”) method in . And that’s why if you check your folder, you will see you have a logs.txt file. Let’s check it:

Solution Explorer

As you can see from images, it saved logs messages to the logs.txt file too.

That’s how we can easily save logs and do that by using Serilog. If you want to learn more about it you can check Serilog documentation from here.

Seq

And there is a visualizing platform which is “Seq” you can add it to the project so with Seq’s user interface you can visualize logs messages and make quick search.

You can access the source code from here, That was all that I wanted to share with you about serilog.

I hope you enjoyed it so if you did don’t forget to claps and follow on Medium. Happy coding :)

--

--

Server Tombak
nacressoftware

Software Engineer with a relentless passion for learning and sharing knowledge. Join me on my journey to explore the ever-evolving world of technology.