Serilog - Saving Logs in SQL Server - ASP.NET Core 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to learn how to use steady log so that we can save log messages into our sql server database so let's create a new project it will be an spdo net core web application i will name it serilog example and you can find the code in github for this example in the description of this video let me say spiro net core web api although what we're going to do will work in any template of an espira net core web application so let me click on create and we're here in visual studio let's go to the solution explorer because first i want to install some nuget packages so let's click on here let me go to browse and we will install three packages the first one is called serilog.hp.net core i will click on here and i will install it and i will click on accept then i will install save log settings configuration let me click on here and also install it and finally we are going to install seri log syncs microsoft sql server so let's click on here uninstall accept and we're going to go here so i can close this now we have installed city lock so let's configure it first we need to put some configuration in a configuration provider in our case we're just going to use the upsettings json file so in here i'm going to write the following i will write here seti lock which is where by default we can put the serial configuration so let me say minimum level and i will say information this means that we're only going to store information warning error and critical messages not trace nor debug so let's put a comma here and let me say write 2 because where do we want to write our log messages we want to write them into sql server so here we are going to do this we're going to say name and we're going to say ms ql server adding here i can say arcs for the arguments and we need a connection string we still don't have a database but we will create that in just a minute i will say data source in my case i can just use dot because the name of my sql server instance is the same of my machine semicolon here initial catalog which is the name of the database we're going to name it seti log example semicolon here and integrated security equal to true because i want to use my windows credentials to login into sql server so i will say table name which is the name of the table and we are going to call it logs and finally i'm going to say autocreate sql table is going to be equal to true because i want that this table gets created automatically now let me create this database i will copy this name and then i will go to sql server management studio i will right click on databases new database i will paste the name here select example i'll click on ok and here we have our database and as you can see we don't have any tables right but that will change once we finish configuring citylock let me save this and now let's go to the program class first i need to come here i need to say use seti log control dot usari log will bring the namespace citylog which is this one that we have here and now this is working and now we need to configure that login provider with the login provider of serilock we're going to be able to use the ilogin service that comes with asp.net core and every time we use that service we are going to be able to send our login messages into sql server let's do that let me say here i configuration root configuration equal to new configuration builder we're going to build a configuration because i need to get from there this information that we have here so i will say a json file settings json i will say that this is not optional so optional false and also reload on change true and we're going to build this and let me put this in another line so that it can be visualized better now i need to use that configuration to configure the city lock logger so let me say log.logger equal to new logger configuration read from we're going to read the configuration from a configuration provider and we're going to pass our i configuration route and let me say create logger and with this we're good to go now every time we use the ilogger service for example in the controllers folder in the water forecast controller class here we have a ilogger instance ai logger service that we're using and when we get to use it in just a minute we're going to be storing the login messages into our sql server database let's do that first remember that we don't have the logs table right so let me just press ctrl f5 to run our application and you're going to see that automatically just as we configure it the table is going to get created let's go here let's right click in tables refresh and here we have logs and let me right click here select top 1000 and as you can see we have a lot of information here why is that well because we selected the minimum level to be information and because experiment core by default is already using the login service to display information level messages we're getting those messages here for example here we have application started press ctrl c to shut down hosting environment development and so on i will teach you how to remove this in just a minute for now what i want to do is to come here i need to come here i want to use this i logger service so let me say logger i will say log information this is a custom message and let me compile my application and now i need to run this method so for that i can come back here and i can copy this and i can paste that here i could also run it from here but i like to do it from here so let me press enter and i have run this now the login message here may take like a few seconds so if you come here and you just don't see it it's okay just wait a few seconds and try again in our case it is here this is a custom message so as you can see this is working now again this is too many information we don't want to have this much of information here so what i can do is to delete all of this so let me say delete and i will hover this and press f5 now let me delete this delete from here and now we have an empty table so let me go back here because i need to go to that settings json and instead of using the minimum level information i can use something like warning which is more reasonable and let me save and now let's come here let me say then log warning and also let's make another example right away i want to say try i want to throw an exception new not implemented exception for example and then catch that not implemented exception and i will say logger log error and i will pass the exception and the exception message x message and that's it now let me ctrl shift b one more time to compile my application and let me come back here and let me refresh now i can go back here and i can press f5 and as you can see we only have these two messages why because we are filtering the information messages we only are showing warning error and critical messages so here we have this is a custom message and here we have the exception the method or operation is not implemented and let me show you something here we have a exception field and i can copy this and i can open notepad and paste that here and as you can see here we have this stack trace and here we get that the error was thrown in line 34 and indeed if we go back here we will have that this exception was thrown in line 34 and not only that there is something else that i want to show you and is the following let me copy this and let me see how it looks here looks okay but we can make this a little bit bigger so let me say 18 i just want you to see the following thing we have this request id field here right this request id allows you to relate a login message with another depending if they were made in the same http request for example let's come here let me copy this and also paste it here and as you can see we can copy these and i can use find and you will see that they are the same request id why because they were made these two log messages were made by the same in the same http context now let me go back here and i want to refresh we just made another http request which is different from the first one of course now if we go back here and we press f5 and let me take another property's value from here from the new http request you are going to see that now we have for example here 8 0 0 0 2 e and 8 0 0 12 which as you can see means that we can definitely definitely use this request id field to relate one login message to another so that if we are studying some sort of case and we want to see which login messages are related to that case to that http request in which the case occurred you can definitely use this request id that we have here if you like this video please make sure to subscribe to my channel and let me know what you want me to cover next thanks
Info
Channel: gavilanch3
Views: 19,600
Rating: undefined out of 5
Keywords:
Id: LwgBWk48ywU
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Tue Mar 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.