How to implement Serilog in .NET Core 6.0 | Enable logging globally | Log using dependency injection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to negra techies this is our asp.net core tutorial so part of this video i am going to explain logging in dot net core application hopefully we all know the advantages of lagging so when you are developing a new application basically we can debug and we can add some console in order to we can get the application progress and also any exception also we will get the complete information but once we deployed we cannot get this all the information so in case if you are using this log so we can check in the log file if there is an any error we can check what is the details of the exception so basically we can get all the information for implementing this log now it is we have the several third party libraries so in this video i am going to explain one of the libraries called siri lag okay first let me provide some details about this siri lag the serilag is an open source.net library and it provides the rich logging experience and also it's run this dartmouth framework and also dotnet core and it's provided for supporting the structural logging and the integration is very simple because the configuration point of view we have to install the package and you have to do some small amount of changes only and the performance also great basically it's not going to affect the application performance so these are the advantages now let me move on this implementation for implementing the sale log in our application first we should install this package serialog.asp.net core so if you are installing this package it will install the required dependencies also so this is my created application it's based on my last video here i'm just executing this package command so it will install the sierra log in my application so look like the installation gets completed now let me go to the one of the controller so in the controller i'm going to add one namespace sirila okay okay the namespace gets included so if you want to use this seri log we have to complete some configuration so then only we have to use in the complete application so let me go to this program.cs file so here also i'm going to use the same namespace so after that let me initialize my blogger so log that library called new login configuration so here i am going to write all the logs into one of the file so let me use this right option then say file so insert the file we supposed to pass our parameter the first parameter is our file path and the second one is rolling interval so rolling interval that day okay these are the basic parameters so after that we can create our lager okay it's done so let me move in the next line okay this is fine so here i need to provide the log path so i am already created one path here so let me copy the same thing i just included here and also i need to provide the file name so apa lag so in the log folder already i'm having one log file so let me remove it the application will create there is no file so here we have done now let me go to my customer controller so in the customer controller the first method the get all method here i am going to use this log log dot log information so here customer get on triggered this is fine so now let me run the application okay our application is loaded so let me refresh it here yeah it's fine so now let me go to the log folder so here we don't have any log file at all so let me try to execute this customer services okay it's returning the data the same time if i'm checking the log path so the log is generated so here we can see the only one detail like a customer get all is triggered the same time if i am trying to access some other methods so it's working but if i'm checking the log nothing is updated because we have implemented the log in the manual way that means so what are the functions it's required for lag we have just included but this is the not right way we have to implement this logging in the global way so let me go to the program.cs file once again so nc.log.logger let me define my locker first okay other steps are almost the same so after that in the builder dot lagging dot add siri log so because we are using this siri log online so here i am going to pass this one now we have enabled this login globally okay so let me stop and run once again okay application is running so now let me check the log even i'm not executed anything so you can see the information details like so our application listening in our two hosts and for stopping the application we have the command like this and our main content for this like that so we will get some basic information so when you are running your application be not provided any custom value now let me try to execute my one of the method okay i'm able to get the value so if i'm checking this log see so i requested for executing this getall method so it will return the customer information and it is executed the particular query and finally i am getting this response also we can see all the informations okay so this is all about the implementing globally uh now i'm trying to access some other methods also that also will capture so i'm just executed this invoice header now see once again and it's requesting for this get all header method and it's executing this particular query from our table sales getter and also once it's finished we are getting this 200 response okay and still if you have a questions like i need to provide some custom messages also how we can do it is so using this dependency injection we can do it so let me go to the customer controller so instead of using this log that information i am going to inject here private read only eye logger and i how to pass this one okay now this is fine so i just injected and again i need to set the value also so now the dependence injection is completed so after that you can provide like this log information so basically we have the several types of logs so the basic oneness information the similar basic one log warning and log error so let me use the log information only log testing ok let me restart the application once again and i'm going to execute this get all method it's returning all the data so let me open this log file also so in the log file so we can see this one the lag testing this one actually we have included so additionally we are getting all the information whether the request is requesting and it's executing so what's the response we are getting so all the information we are capturing so as of now we are seeing this information type only so let me get the error type also for creating this error what i did in the so in the container side the customer container so in the get by code method so actually i'm getting the string only so i'm trying to make an error convert dot to 32 so i am trying to make in the integer so let me run the application once again so before that i am going to delete this log because the length is too high dotnet run okay application in running mode so again the log file is generated we have this basic information now let me go to this get by code method here i'm going to provide so this is one of the string value if i'm sending here i'm basically i'm getting this error response okay so if i'm checked in the log also so i will get the same information and this exception type is error only error now just to consider own scenario instead of capturing all the events if there is only error situation only we supposed to capture the log means b how to set the minimum error level so that we supposed to do in our program.cs5 before going to write the file i'm going to give minimum level so in the minimum level if i'm giving the information it will take the information even there is an exception or warning that also will cover but if i'm providing this error it will capture the error only so because this is the higher priority one so i'm just added okay this semicolon is not required so let me save it once again and i'm going to delete the log also okay i need to stop the application then only to allow to delete it okay fine so let me rerun the application okay here after uh it will store only the exception information only so if i'm running the application this is just a success scenario so it won't capture this information if there isn't any exception that time only it will capture so let me check the folder there is no file okay and now i am trying to execute this guitar method so this is also positive response currently we don't have any exception so in the folder also we don't have the file now let me come to the get by code method trying to exceed the same method it will show an error okay the error scenario if i check the folder i can see the lag file is generated so here i can see this lag information only other information not stored here so this is the function it is called applying the filter for log level okay so as of now i just explained how to apply this uh lagging for individually and also how to enable globally and one more thing how to applying the filters based on the lag level so now let me explain how to move these settings like a rolling interval and also this log file settings so these all the information how we supposed to move in the app settings.json way because if you have all the information in our program.js file if there is any change we have to build and deploy once again but if you have something in our json file we have to just to modify the json file in the server itself there is no build is required so let me do it for doing this one let me go to the app settings.json file here already we have this logging and allowed hosting the connection string we have added so after that i am going to use cdlog okay so then i am going to mention which dll i am going to use so using so serious skins dot file so next we have to provide the level of the error we are going to capture so the default value should be error or else we can provide the information and the next thing is we have to write the file here i am going to provide the upset so the first parameter is name name is justin file so next i am going to provide the argument so this is also not the object only so here the first input is path so the path already we are having in our program.cs file so let me copy everything add it but the next one is rolling interval so it should be day and we have one more property it's the output template if required we can use it so already i'm copying it so let me include it so this all about the changes in our app settings.json file so now let me configure this value in our program.cs file so in the program.cs file i am going to comment these two lines so after that i am going to read the settings from our configuration so so let me add a read from that configuration of builder.configuration so let me provide ender okay dot enrich from log content okay we have done our changes so let me go to the log folder here i'm going to delete the file once again so look like our application in running mode okay it's remote so again we are capturing the exception in the error level online so that's what i included like this so let me run the application it's saying the error is serilog dot skins dot file so actually it says synths okay let me re-run the application once again so application is running so if you want get more information about the siri log so you have to visit this official site of sirilux seriala.net so here you will get the complete information like the installation and the configuration basics so if you check here they will provide all the information what are the packages you supposed to install it so we need to install the serial log since that console but if you are including shellac.asp.net core so it will include all the dependencies okay and the configuration inside what are the things we are doing the same information they have provided with very detailing so you can refer it okay our application is running now so let me check in the log folder there is no file because we are maintaining the error so in the log folder we don't have any file because we are maintaining the logs for only the exception so let me refresh it again i'm going to use the get backward method online okay the exception is coming see again i am able to see the log and it contains the lag information so now if i want to get the complete log information then i have to change the settings so the first thing is if i can command this one then it will provide all the details so let me cut it again i'm trying to run the application dartmouth run so now let me check the lag so you know i am getting all the information so application listing in the local ghost and it's the development environment so what is the root path i'm getting all the information and also if i'm trying to execute something so the catalog method so if i'm checking i will get all the information okay so all are the information type only basically the good practices you have to keep this minimum level and inside that you have to provide the exception level or else the lag level so let me re-run the application once again control c i'm going to delete it okay again application is running now so see i'm getting this information i am trying to execute on new service here not a new service the default service the weather forecast service so here also i am getting the complete information i am trying to access this weather forecast so the method is get method i am getting the response is 200 so it will capture the log globally so this is all about completely handling this log information and let me show you the code so we are did some configuration in our app settings.json so after that we have just enabled in our program.css file actually we have just created the logger and then we have included in our application globally so it will act complete application and also uh this share log is supporting for the different applications so if you refer in the official site you will get the more information and you have to check these things so you can see it's working for amazon cloud watch and the cloud the console application and actually we are using this file option we just used this serial dot sync start file and also we have some interesting features like a new relic so the new link basically provide and kind of ui the ui itself we can differentiate and we can do the search operations okay and one more thing we can connect the databases using this database connectivity we can store the log information in the table itself and this microsoft streams so here we have to notifying the particular group so if there is any any exception we can notify the group so these are the various features was there for getting the more information please refer the documents in the official site of siriland.net okay thank you thanks for watching this video still if you have any doubts or clarification please post in the comment box and also please don't forget to subscribe my channel thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 20,252
Rating: undefined out of 5
Keywords: log implementation in dot net core 6, serilog in .NET Core, how to implement serilog in dotnet core web api, how to configure serilog in .NET Core, how to write log file in dotnet core using serilog, features of serilog, dotnet core using VS Code, How to filter log level in serilog dot net core, steps for implement dotnet core, How to add serilog in .NET Core, How to incorporate serilog in .NET 6, Serilog installation in NET Core 6.0, how to capture only error in serilog
Id: OrzcJw4IOQc
Channel Id: undefined
Length: 23min 8sec (1388 seconds)
Published: Thu Jul 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.