Structured logging using Serilog in ASP NET Core Web API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube viewers hope everyone is doing well this video is about how to implement structured logging using citylog in asp.net core api but before we implement serilog let's talk about why logging is important logging is important for the following reasons logging provides breadcrumb trail for activity leading up to an event event can be good or bad lolling can help supplementing exception information recorded in other systems logging can help understand how clients are using our application or api logging can record application matrix what is structured logging in older systems log messages were simple strings you could not use them for any kind of analysis structure logging is a modern approach where logging events are treated as structured data rather than just tech difference between structure logging and string logging is the attributes identified and structured so that a system that understands these types of logs can perform special operations and logs such as filtering sorting based on the attribute why serilog in dot net space there are few libraries like n log log for net and of course very long comparing the features of all the libraries is autoscope of this video but the main reason for choosing sirilog is that because it is newer it supports structured logging out of the box and there is a lot of support for study log in the community leading to a lot of extensions and logging things stereolog can be used in classic.net framework applications and for applications running on latest and greatest.net 6 or data and 7 beta i have used serilog in almost every dotnet application i have developed and i was very happy with the results i like the simple integration of study log into dotnet applications ease of use and how simple it is to start using it let's create an api and integrate study log open visual studio click on create a new project select the asp.net core web api click on next give project a meaningful name for example city log example you can change the location and solution name if you like to click on next uncheck configure for https and uncheck enable open api support click on create once the project is ready close the overview tab and to start using city log we have to install using uh the package manager so right click on the solution select manage nuget package go to browse search for very log asp.net core select the first package in the list click on install click on ok click i accept and once the package is installed close the package manager and go into program.cs file and import using studylog namespace and for the builder dot build line type builder host dot use study log and in between the brackets another set of brackets type context and another second parameter is logger config lambda expression logger config.right to console brackets save the project run the project and let's see what kind of logs we are getting so as you can see we are starting to get some logs and we are also seeing logs which are not even captured by the default logger so as you can see these logs are plain text so you cannot do any kind of manipulation or any kind of analysis on it so let's add or tell our logger to start using the structured loggings close the console to stop the project and in the console brackets type new json formatter brackets close and include using stereolog or formatter formatting or formatter or json save it now run the project again and now in the console you can see the logs are in the structured format or they're much more better structured as you can see there's time step included and level and now we can take it to any kind of logger and we can do any kind of a lot of manipulation on it or do kind of analysis on it let's configure additional things to view and analyze logging data in different ways there are too many syncs available to list but we are going to look at file sync this is useful when we don't want to ship the logs directly over http or https and want something else to ship the log files or logs for example to datatalk we can use datadock agent to ship the logs to datadog from files to set up the sync we just need to install the package let's close the console to stop the project right click on certilog project and go into the package manager and search for studylog.sync dot file select the first package now click on install click ok close the package manager and right here in the same user log line type write to dot file and the path of the file for example c backlash logs slash steady log example dot txt david and now let's run it fix this issue i'm missing one slash here now save it run it and let's go to the folder and see the log file backslash logs as you can see the log file is there open it in any text editor as you can see the logs are being written to the log file but as you can see these logs are not structured uh they're plain text so we cannot analyze or do any kind of manipulation to on these logs so let's go and write fix it or change the code to write the structure log into the file close this stop the execution of the project and before the file path type new json formatter and capital f and now let's run the project let's examine the log file again open it in in-text editor and now you can see that logs are written in a structured format that's how you add structure logging uh using siri log to console or to uh file sync and there are other syncs as well which we can be used in conclusion we have looked at how easy it is to get a structured logging setup using celerilog in asp.net core there are a lot of things available for cerylon we have only touched the surface of the possibilities of celilog i encourage you to read up on the extra things can be done to make the logging even better that's all for this video i hope this video was helpful and if you find this video useful please subscribe as always if you have any questions comments or feedback please leave them below click that like button likes are always appreciated please subscribe to get notified whenever a new video is uploaded thanks for watching i'll see you in next video you
Info
Channel: TheTechPaper
Views: 555
Rating: undefined out of 5
Keywords: Structured Logging in ASP.NET Core with Serilog, Logging in .Net Core, How to use serilog?, Using Serilog, Using Serilog in .Net, Using Serilog in .Net Core, What is serilog?, Structured logging in .net core, Sinks in serilog, How to integrate serilog in .net core web api, Logging .net core web api, Logging .net web api
Id: Nu9QOL3wmnM
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Mon Jul 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.