Serilog ๐Ÿš€ Logging in .NET 7 Made Simple & Fun

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends let's talk about the very exciting topic logging today I know that logging is a topic or a feature or an implementation you just have to do at least for many of you it is for me it definitely is but there is one solution you probably already heard of it that makes logging really easy and maybe also a bit fun and that's Siri lock so I wanna show you an introduction of sarilog how easy it is to use this library for your logging no matter if you want to log into the console or to a file or if you want to use Azure even for that stuff and I'm pretty sure you can do this in only a couple of minutes so without further Ado if you like this video I'd really appreciate it if you hit the like button maybe even subscribe to my channel it does make a difference thank you very much for that and if you want to get more tutorials like this one into your inbox then maybe you want to check out my newsletter thank you very much for considering and now let's start with the tutorial so the first thing you need to make cereal work is and you get package and the way sirilock does work is you can choose two either get this package here simply called serilog and then when you want to for instance lock your stuff into a file so store your logs into a file on your hard disk for instance or into a console then you already see that cellulock organizes that stuff into so-called syncs right so a file is one sync a console is another sync so this is where all your stuff all the lines you want to write uh Landing in essence and where you can put all that stuff but that said you can install Siri lock and then the additional packages for file and console if you want to lock your stuff into that thing but I've got a web API here an example project typical really the default template for a web API a.net core web API 2.7 in this case and if you're choosing to do something with a web application then you can also or if you're doing something with asp.net core in essence you can also just grab this package here and you've got everything covered so Siri lock aspnet core perfect if you want to use serilog for your logging in a web API project for instance and this is why I am choosing choosing this package here so we install that and then great stuff is you can already go for instance your weather forecast controller and let's say we just want to lock here the the result of this function here right or of this endpoint so what we can do is first say our results is this stuff actually and down here we want to return this then so return the results all right and then here we can enter lock almost lock all right Visual Studio control period and then we see the namespace already for Siri log so we add the reference here and if you wants to it added the namespace the reference perfect and then we can already say Okay I want to lock something with the information level or the error level or debug whatever you want to do you can already do that here and let's say I want to lock whether four costs in this example is the following then I can do that right and now maybe you're asking if you don't know that already wait a minute result is a list does this really work that it is displaying the complete list not yet but what sirilock does or what it has is a great feature and that's the so-called destructuring so it really destructured these structures any object you want like the development Tools in Chrome or Firefox for instance do it when you just use console log in JavaScript and then you put an object there then you see the complete Java the complete Json object I mean and what you can do is you simply add this add sign here in curly braces the the arguments or the parameter and then you also add see it here the property values then right so the first thing it is this is the message template and then you additionally add all the properties you want to add there the values so in our case that would be simply the result okay when we do that maybe you're already wondering really is this enough well not really there's one more thing we have to add but let me just show you I did nothing else and now I want to have a look at the console here and this then would be the the web API example right we've got the weather forecast endpoint you know this we hit execute and now we have a look at the console nothing there and why is that well we have to configure Siri lockbot please don't stop the video it's really really really easy and fast so what we now do is we go to the program CS remember we've got the you get package we've got one line to lock that stuff uh the better forecasts and now here in the program CS we add our configuration and we do that for instance right here so lock it is again using Siri lock and then we say we wanna add a new logger configuration that's the one and there's a whole lot of stuff you can configure I really recommend opening the the documentation if you want to know more for instance here just Google for it and then you you come to the GitHub Pages this is how you install that stuff right and then here you also see some configurations because this is pretty much the same thing that we will do in a sec but there's of course more as always so let me just continue here we wanna uh lock the for instance minimum level debug then or we can also say information for instance right then the next part we want to do is we want to write this to the console and then we can already say create logger all right so we remove this semicolon here of course and now the error is gone we save everything start the application one more time here's our console and here's that stuff and now try this out we hit execute and what do we see wow we see weather forecasts and then really all the forecasts here de-structured in essence isn't that great and that's already it so this is how you can do it for your development case for instance you just want to log your stuff into the console but again you can do more with that for instance lock everything into a file and for that we can simply say write two and then I want to write that into a file and here I first enter the path so where should this go if you want to do that you're on Windows and put that into C and then a certain folder then you would start with a slash and then locks for instance or if you want to use your application folder then you just remove that forward slash and then you add logs and then for instance my beautiful log Dash and why the dash because by default um adds the current date then to the to the to the name of the file if I also add this a little parameter here the rolling interval meaning I can say I wanna have a file for each day our month whatever and that's that right and then the resulting file would look like that I've got logs and then my beautiful log Dash and then the current date and then the next day I get another log right so this is how this works we just restart our application and let me open that real quick here's the development folder let's say and now I go back to the application run this again and now I've got my locks folder isn't that nice with the lock we can open that and see it here and now if you want you can of course also open this with a visual studio code for instance and if you've got a code formula installed like prettier we can also say wait a sec this is a Json and now we can also format this document with let's say the Json language features in here I've got the objects isn't that nice and with that if you want to go more into the direction of structured logging you can play around with that and say well all the logs look the the same or the the template or the structure right Orthodox is the same and then you can even have a look and compare the the objects here isn't that nice so this is what you can do with Siri lock but there's one more thing I want to show you because I I already hear the questions well this is hard coded into the program CS what about a different development stages for instance we've got we've got our app settings Json file here right but we've also got an app settings development maybe you've got an app settings production app settings test whatever it is I get it you can also read the configs for Siri lock from your app settings Json file and I'd say we do that real quick so in it instead of that stuff here what you can then do is you say read from on our street from and then configuration and here we can add our Builder configuration and that's in essence our app settings Json file I've got one thing create logger all right and now here well we have to type some stuff but let me do this real quick we can in essence just remove this section here and instead we add another one called serilog all right and we open this thing here let me just remove that stuff all right and here now we say we want to use certain syncs again right you remember from the beginning so what we want to do is we want to write that into a console and into a file and that's why we add here Siri lock sinks console and then pretty much the same thing for file logging all right so that's that and then we have to configure we can configure for each sync what we actually want to do well the easiest one is the console sync because this one again we add an array here uh well this only gets a name and that would be the console but regarding the file again maybe you wanna add the rolling interval and the path of my file right so in that case we say again first the name and that would be the file and here now we add some arguments and again I highly recommend going to the documentation for all the stuff you can configure here the first thing in our case should be the the logs so again the locks folder my crazy config log from the app settings right Dot txt and again the rolling interval would be day all right and that's really a tiny configuration but this now should also work so let me just restart this again go to our folder here but now let me open the web app try this out no let's let me hit execute a couple of times and I'm gonna go back we've got the My Crazy config log open that again and you see here's also the data isn't that nice and the last thing the very last thing I want to show you if you're wondering well it's nice that we can write only this little line here into this function or the end point the body of this this function here but what if I want to get an information every time a user is making a web service call do I have to write this line into every controller method or server method well of course you don't if you want to get the parameters yes then you maybe want to but if you just want to get the information hey that was made a call and it took that long then you can do something else and that would be using sterilog's request logging so how does that look well and just add one additional line here in Builder hosts and then use sarilog all right and then the first middleware I recommend you should add is app use Siri lock request logging all right let me just save that restart the app one more time and let me just remove all the locks here can do that because it's open somewhere or it's already writing that oh yeah it is already writing something of course but let me let me just show you first the application so here now we hit execute and then I open this again in Visual Studio codes all right and let me set this to Json so this looks a bit better but you can already see here that something is happening right we've got the information logs and it says request starting well this is the startup of the application really but here now we see route matched with action get controller with a forecast we see this is our lock here the the data in essence but again you get lots of other information if you use the request logging for instance the response times 47 milliseconds let me try this one more time we hit execute and you see something was added and now it was 26 milliseconds after warming up right and here the next one 50 milliseconds okay 20 SEC milliseconds all right so this is how this stuff works I think that's awesome and this really is just surface level so you can do a lot more with Siri lock but again just for this little introduction here I hope you learned something and you already think this is great Patrick I want to use this and I want to see more if you think that then please tell me that in the comments and I'm happy to create another video for you and apart from that I would really appreciate it if you hit the like button subscribe to my channel and maybe you want to watch the other videos here you see on the screen so thank you very much for watching and I'll see you next time take care
Info
Channel: Patrick God
Views: 3,063
Rating: undefined out of 5
Keywords:
Id: QjO2Jac1uQw
Channel Id: undefined
Length: 16min 23sec (983 seconds)
Published: Tue Jun 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.