How to Use Serilog in ASP.NET Core Web API | .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
logging is an important part of any application as long as everything runs smoothly you don't need it but when you face any issue with your application you better have logs that help you find the problem in this video we will learn about serilog and integrate it into an asp.net core web api application running on.net 6. [Music] hi and welcome back if you're new to the channel i'm a software engineer with more than 10 years of experience with the net platform on this channel you learn all about.net development let's take a look at serilog zerilock is a net library that provides diagnostic logging to files the console and almost everywhere you would like zerilock can be used in classic.net framework applications and for applications running on the latest and greatest.net seeks one of the biggest strengths of zerilock is that it has been built with structured logging in mind you can find more information on cereloc's website linked in the video description now that we know more about seriloc let me tell you my personal experience with it i used cereloc in a few applications with different logging requirements and i was always satisfied with my results i cannot speak much about performance because i don't work with large distributed ultra scalable applications but the performance was never an issue in my use cases i like cereloc simple integration into the net applications the ease of use how simple you can start and what more advanced features are available and can be used later in your journey let's create an application and integrate serilock in visual studio 2022 we create a new project based on the asp.net core web api project template we choose sirilog demo as the project name and on the next dialogue page we stick with the defaults and use.net 6. we click on create and wait until visual studio generated the project for us installing seriloc is simple first we open the nuget package manager and search for the seriloc.asp.net core package and install the latest stable version after a few seconds zero log is installed next let's integrate serilog into our application by registering it as a logging provider i insert the code snippet that defines a new logger configuration we configure the configuration to read the application configuration from the application builder we also enrich serilog with the log context next we clear all existing logging providers the web application builder adds for example the console logging provider and we want to get rid of that last but not least we add serilog to the logging providers for our application and provide the configured logger configuration object as its sole argument we configured the logger to use the settings from the application configuration let's open the appsettings.json file and configure cerelook i insert the sample configuration let's take a look at it first there is the using property where we define an array of log targets in serilog log targets are called syncs we configure an array with a single sync the seriloc.syncs.filesync next we can configure log levels and more importantly to write to section in the write to section we configure information for the file sync the name property with the value file defines that the property provided in the orgs property will be used for the file sync we configured a path where our log file should be written the rolling interval property defines when a new file should be created and the output template defines the structure of the log output for each log statement now that certilog is configured we open the weather forecast controller class and insert a log statement in the get method now let's start the application we use record to send a simple get request to the weather forecast controller open the entry in the endpoints list and click on the try out button we click the big blue execute button to send a request we receive an array with weather data however let's close the application and look at the log files we open the project directory and open the logs folder the file name contains the prefix we configured in the appsettings.json file and the part after the dash is the current day with the current configuration cerelog creates a separate file for every day we collect all the log statements for any given day in its file let's open the file and scroll through it we can see a timestamp and our log statement that we added to the weather forecast controller class now that we learned about the basics of cerilock and integrated it into our application let's look at the provided syncs as we can see there are things for popular cloud providers including amazon cloudwatch azure analytics and many other cloud services there are also syncs for the windows event log microsoft themes and other interesting log targets you can find the link to the provided syncs in the video description you can enrich serilog with additional information such as a session id or a web request id you can configure the output format with placeholders and configure json instead of plain text output you can explore all the additional opportunities serilock provides in the wiki on the github project site linked in the video description if you have any questions or want me to cover additional server lock features in a future video leave your comment below thanks for watching remember to subscribe to not miss out on future.net content and watch this video next [Music]
Info
Channel: Claudio Bernasconi
Views: 33,766
Rating: undefined out of 5
Keywords: Serilog, Serilog Tutorial, Serilog Tutorial C#, Serilog C#, Serilog 2022, Serilog Asp.net, Serilog Asp.net Core, Serilog .NET 6, .NET 6, C# Logging, dotnet, dotnet 6, dotnet Serilog, How to Use Serilog, How to Use Serilog in C#, WebAPI C# Serilog, Claudio Bernasconi, C#, asp.net core
Id: MYKTwvowMUI
Channel Id: undefined
Length: 6min 24sec (384 seconds)
Published: Wed Jan 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.