Serilog Tutorial - Starting out with Serilog in ASP.NET Core (Blazor app)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is julian and this is how you install sirilog in an asp.net core application more specifically a blazer web application so let's get started and if you've never heard of it you might ask yourself what is sirilog sierra log is a structured logging framework used to write to files console azure blob storage sql server databases you name it all these places are called syncs and a sync is just a term to describe a place to write the logs into and if you're new to software development you might even ask yourself what is logging in the first place and logging is just a process of recording some representative data about your application say the current state of a function or some exception details that you want to record in order to later find the root cause of the issue for example say your brand new web application has got a bug in recently introduced in it and that happens on the login page for instance and that prevents the user from logging in and that's really bad and the only thing the client is presented to is potentially a 500 server error and that's okay you don't want any information to get back to the client having said that you might want to log those exception details in the sql server database which we're going to be doing today and use those exception details those logs to later get to the bottom of the issue identify the patterns and ultimately fix the bug there are two types of logging there is basic logging and structured locking and basic logging is exactly as the name implies is the most basic way of generating logs and that could literally be just a piece of string without any other data any other type of data attached to it however structured logging is bound to a specific and consistent format across a web application that can later help with identifying patterns and for analysis purposes okay enough with the overview here's what we'll do as part of this video step one we'll quickly set up the blazer server web application uh then step two we'll install a package the package that we need which is sierra log sp net core normally in a simple console application you won't need this package however we want to simulate a real world environment and that's going to be a web app if you're on this channel and you want to be able to um install uh logging and use logging uh yourself step three i'll show you how to set up sirilog to write console as we'll be generating some logs for the console then step four um we'll be setting up a serial log to write two files and we'll generate a couple of logs for some files step five set up a sql server database with entity framework core and we'll install a package called microsoft entity framework core sql server obviously and we'll create an initial migration that will have our database and our brand new log entity generated for us more about that step five in the video and then step six and the final one installing um the microsoft sql server sync serial because we'll need that to be able to generate the logs straight into our um log table in our sql server database so that being said um let's proceed with the video so the very first step that we'll need to do is open up visual studio 2019 i've got the community edition here and then click on create new project then as soon as it loads just click on creating a new laser server app or if you can't see it in the recent project templates section just search for laser server yep so just pick it from the from the list i'll just give it a name and then choose a path um just the thing to note make sure that when you set the project name uh you don't set it to serialog otherwise it will be hard for you to install packages because one of the packages that we will install is obviously going to be serilock so just make sure that you name it something else other than sirilog maybe name it like myself so let's carry on as for the framework i'm just gonna leave it as network 3.1 no authentication and click create okay um so we're in obviously i skipped the part where it was loading um for time purposes um and now that we're in i'm just gonna do a brief overview a very very brief overview of what the app has got and where we will be working in so obviously we've got the shared pages and the main pages and we will be working in the fetch data actually i think it's just better if i just run the app for you so you can basically see how it looks like if you've never seen a blazer server app um the actual basic template um so yeah yeah so this is it um here is where we'll uh we'll uh we'll do our logs uh so we've got obviously we've got the home page uh the counter where if we click just counter changes and we've got the fetch data and every time we fetch the data it's supposed to be different so yeah as you can see the summary is different um every time we do it um so we'll make it so that we'll obviously we'll add some system logs uh but also we'll do some custom logs so that every time we click a row uh we'll add a new log um yeah so let's get started first things first what we'll need to do is install uh siri log for asp.net core so that's step one um so if we go in google chrome uh this is the very first thing so if if you're not already on this page um you can just type in sierra log dot asp.net core core and as soon as you do that uh the very first package from serilog will be sp net core and what we'll need to do is i'm just going to choose 4.1.0 uh copy this and go in the terminal make sure that you actually cd into your project which is this by doing cd and then pasting in two single quotes pasting in your uh project path and then hit enter uh and we're there so here what you can do now is copy this and then paste it in here if you don't already have.net installed just make sure that you install that on your pc and this should do the trick um obviously let's see a second way of doing this so a second way of installing serial log asp.net core is by going in your project expanding your client project and then going into dependencies right click manage new get packages go to browse and then type in the same thing sirilog dot asp.net core and then the very first thing should be um our required package uh and as you can see the latest table is 4.1.0 so we're just going to install that so this is exactly the same thing as we would have done by um executing that command right here it's exactly the same version so yeah so that's just one thing to keep in mind that's installed now right and we can double check that it is installed by expanding the dependencies uh and then expanding the packages and as you can see we've got serial of the asp.net core and inside of it we've got cereal and um a few things so like i said we've got a console thing we've got debugging we've got a file sync and these two ones are we are going to use um throughout this project cool um step four so setting up the program program.cs file with um a semilog and if we go in the root of the project we are we need to open up program.css so i'm just going to collapse this for the time being okay so in here what we'll do is here a few great points and then log not breakpoints but more like spaces new lines um obviously it will not find a serial logger by default so we're just gonna put download dot and then press ctrl dot and then say using siri log so that's it so now we're using siri log um alternatively you can just put using server log at the very top uh from from the get-go to make sure that it actually finds what you're um what you're about to type in so log.logger goes to console and create logger that's it um so we've instantiated the so we've instantiated serialoger uh what this line does is actually just writes the console so it configures uh serial log to write to console and then this one uh says create logger so there's the instantiation next we'll wrap this create host of host builder in a try catch block just so we can look if something goes wrong we can we can actually look what went wrong um exactly what i said in the in the beginning of this video so i i type in try and then tap two times to have this entire block created for me what i'm doing now is just logging a piece of information to say uh to the console starting the web host and then i'm just gonna paste that line in uh and then in the catch block i'm just gonna define an exception uh variable right here and then we're just gonna lock that as a fatal to say and then we load the exception and then say host terminated unexpectedly deadly if i can type uh we don't need a throw here and then finally we do log dots close and flush that's it that's pretty much all we need okay so what we'll do now is uh run the app and to be able to see this couple of nice little logs we'll run this from the terminal and to do that we'll uh basically do um net run watch and this should do the trick so now the app will start and as you can see we've got starting web host um and that's it that's pretty much it we won't see the host terminated unexpectedly because it didn't terminate unexpectedly so everything went fine so um yeah that's pretty much it with logging to the console okay next up is setting up dot cs uh setting up with uh siri log to uh log to files uh and to do this we just go in the program that's here so we go in the logger server logger uh configuration and we just type in here write to file and inside here we just the first parameter is the path to um to the log file that we want to log the logs into so i'm just going to put here log dot logs dot slash log.txt and then the second parameter is the rolling interval so rolling interval interval dot day and that's pretty much it so this entire so that what what this means is that uh each day will have a new log file right so all we need to do now that we've set this up is to basically run the app so that's what i will do i will run it in the with the debugger this time and i will open up so as you can see i haven't got any uh folders called logs uh but then as soon as the app actually starts we'll have a logs folder auto generated for us and if i open that up we've got our log and and the date because we've put the rolling interval to be as day so we won't have any daytime if we were to put the rolling interval as hour or minute we'll have the the time as well as the date here so yeah as you can see we've got starting web host we've got the exact same thing as we had in the console but now we have it in a file right so the very last step i'd say step number five um if i haven't numbered them correctly uh so far i do apologize um and at this stage we'll have to uh do some prep so what we'll need to do is ultimately the goal is to log to a sql server database into a logs table as we said in the beginning of this video so to do this we'll need to um install microsoft sql uh microsoft entity framework sql server package then we'll need to install obviously microsoft sql server entity framework called tools then we'll need to create a log entity then we'll need to create a migration run that migration and then we can set up serilog for writing to sql server database so we'll do that because um potentially some of you haven't uh done that already and i'm doing this tutorial from from scratch and i'll show you how to do it um okay so let's go and install a few packages uh and that's repair here microsoft entity framework uh core sql server uh we copy this we paste it in here and we install the package alternate alternatively like i said in the in the beginning you could have gone into dependencies right click manage nuget packages and browse the package from here it's up to you how however you want to do it it's exactly the same thing or it should be exactly the same thing yeah so we've got that there then the second package is enter framework core tools and this will allow us to create migrations and update the database and as you can see enables commonly used commands at migration drop database get db context what we'll do we'll basically just use add migration and update database so let's install this real quick okay that's done and then the last thing is sierra log syncs microsoft sql server we'll install this we don't need this for the time being but we'll need it at some point um therefore i'm just going to go ahead and install it anyway uh just so we can get rid of package installs for ever at least in this project so okay we've got all those dependencies installed uh now the next step is to add our uh logs model okay so let's create a brand new folder and call it models um ideally you would have a separate project for entity framework for the database context however we're just going to keep it simple here so inside this folder will create a new class called simply log and this is very important um here we'll provide the definition for our logs entity and this is where our logs will be stored so it will have by the way if i type if i type prop and then double tab it will create a simple property for me and this will be an integer called id and this is the primary key then message then a message template then the level this is either informational uh debug um whatever it was fatal there are a number of levels that serial provides depending on the type of message that you want to send daytime and here we've got a timestamp of where the log this is very important for debugging purposes the exception as a string and then last but not least the properties okay so it's like i said it is very important that you uh keep this uh structure intact because if this is changed serilog might not be able to recognize your entity and might not be able to log in in your in your brand new table um okay next thing we'll have to do is go in the and create an application db contact inside the data folder if you haven't got a data folder already set up for you go ahead and create it but we've got already so we'll just create a new class called application db context right and this will inherit from db context and will import into framework core um i will type in ctor to create the constructor and then in here this will be an empty constructor by the way but it's important to set up the parameters so db context options this is more like over a boilerplate code that you will need to write in order to have your database correctly instantiated so application db context options right um so the constructor is done oh forgot to save this uh and now what we'll need to do is link our uh log entity to the application tv context file uh and to do this we'll just create a new property of type db set of type lock and we'll call it oops we'll call it locks this is how we will find that log um entity throughout the project uh import serial obviously oh sorry not 0. we'll import models and this way we have referenced our brand new log entity to the application db context um next thing we will need to do is go into appsettings.json and define our localhost connection string so i'm just going to copy and paste that because it's it's just standard okay that's it so connection strings just make sure that this connection strings is the same throughout the project so when when you define a new property in your app settings.json just make sure that you actually copy and paste that where you want to use it otherwise it won't be found and you will have errors and i just define my localhost default connection string right so the next thing we want to do is go into startup.cs and it is very important to add this line in the configure services function um at this line right here that it says services appdb context application of type applicationdb context use sql server uh and we'll have to import into entity framework core and this actually uses default connection so if we go back to um to app settings.json and what i did is just copy it and pasted this here and it is very important to do that otherwise it will just not work um yeah so this is how you set up empty framework core in a network application um then the next thing we'll have to do is arrive our very first migration so if you have installed the um entity framework tools package you will be able to do something like this dotnet ef migrations add initial create this will not error if you have installed that package correctly and your version is correct obviously so what this will do is take a snapshot of the entities that we have linked to our application tb context and it will create a migration um a migration file that we can then uh execute against our localhost against our server to create uh the database and the entities needed and in this case it's just going to be a simple database and yeah just the logs entity so yeah once that's done you can go inside your project and we've got a migration folder uh created for us if we expand that this is our initial create migration that we've created so as you can see it's got a migration builder create table the name of the table is logs and here we go this is the table definition for sql server to pick up and create that to generate that entity for us and to add to create the database and the uh a new table for us all we need to do is write dotnet ef database updates and hit enter and fingers crossed everything we've done is okay then this should basically create our table and um uh database for us i will quickly go in sql server and show you that we haven't got that database that is called siri log underscore toot we've just got the test database and this is done so if we refresh the databases again we've got our brand new database here and if we expand the tables we'll have a logs table hurray so if we just select top thousand rows i'm expecting this yeah so this is empty now and all we need to do now is actually let's just go in the um in the pages in the fetch data and add a new let's add a new function uh to say log dot information row was clicked and then we'll go ahead and do a using at the top um siri log that's it um so this should just uh work now uh okay so that every time we click a row um then this log message will be uh will be called okay um now that so just to recap we've created our database we've created our logs entity what we'll need to do now is actually we've installed uh the siri log microsoft sql server sync and we'll need to set that up that is our last step and to do that we'll go in the sorry in the program.cs file um and we'll need to create a config object uh this is this is going to hold uh the configuration for our uh sirilog microsoft sql server sync um so we'll do a new configuration not binder configuration builder yeah add oops add json file this will be our app settings.js then optional false then build following we'll do a write to microsoft sql server so we'll configure the sync and inside the parentheses we'll do connection string and we're sitting that config dot get connection string and this will be our default connection so copy and paste that in there then we'll have to define the sync options and this will be a new microsoft oops here ms sql and this is not defined because we're not using it yet so what i will do is i will remove this and then add the using at the very top and the table name will be logs so this is how we tell right cool so this is how we tell um sierra log what table we are going to use okay i forgot to put a semicolon in there uh by the way we'll need to do this as well at the very top of our config function so minimum minimum level dot overwrite and then we'll overwrite for microsoft and then log oops event level dot information and i don't like having this noise here so i'm just going to remove it and import it at the top cool right so all we need to do now is basically run the app and see the results so we've got remember we've got those two um custom logs up on the fetch data razer page uh so we'll click the row basically to uh to generate those uh logs um just as a thing to note is i have already done this off camera and um i had some logs generated but i deleted them so that's why the ids will be uh a bit skewed uh you'll see what i mean in a second okay so i just clicked a few rows now if we head over to sql server and hit f5 you will see that the host is being started and then i clicked a few rows so the reason why it started with id8 is because like i said i've already generated some rows off camera because literally the camera was off during that time and i had to delete them for the purpose of demonstrating this particular exercise right yes so that's pretty much it for today's video on how to install and use uh siri log into your asp.net core web application uh if you did enjoy this video make sure you hit subscribe and hit like because it really does help a lot this channel and until next time yeah stay safe
Info
Channel: Code with Julian
Views: 3,986
Rating: undefined out of 5
Keywords: serilog tutorial, serilog tutorial .net core, Serilog tutorial c#
Id: ad0IhMFsxyw
Channel Id: undefined
Length: 30min 40sec (1840 seconds)
Published: Sat Aug 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.