C# Logging with Serilog and Seq - Structured Logging Made Easy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
structured event logging is so much more than just writing a message to a file since dotnet core natively supports structured event logging I want to introduce a logger that takes advantage of these additional features Sarah log is an easy configure and easy to use logging system that you can plug into a dotnet core project within minutes today we're a configure Sarah log see how to use a basic logging scenario then take advantage of the structured logging by hooking up a visual event sync called seek or seq the source code for today's video is available to download using the link below in the description you'll also find links there to sign up for my mailing list and for my patreon just last week MELAS numbers and patreon members receive special discounts on my courses that were not offered anywhere else now if you haven't come across me before my name is Tim quarry and it's my goal to make learning c-sharp easier one of the ways I do that is by teaching context there are a lot of tutorials out there that teach you what to do I go a few steps beyond that to show you when to do it why you should or should not do it what pitfalls to avoid and what the best practices are basically I get you ready for the real world if that's the type of training you're interested in subscribe to my channel and hit the little bell icon to be notified when I release new videos ok let's go over to visual studio and we're going to do today is we're going to create a new project that is a pet net core web application this is the easiest way of getting started we could also do a worker service because that also has the built-in logging it'd be a little bit harder we could do it a console app or even a desktop app those just don't by default start up the logger in the same way and so you have to do a little bit more tweaking I wanna get right to logging so around do an asp net core web application hit next we're gonna call this the sarah log demo and a seer log demo app you click create and the purpose here is of course not to create a really full-featured web application it's more just to create an application that allows us to do some logging now I'm going to choose a web application that is configured for HTTPS but no docker and no authentication now right now it's dotnet core 3.1 that's because i'm i've got a preview version of visual studio and it won't let me choose dotnet core 3 down on this drop down anymore which is a little frustrating but it's okay because 3.0 will work just the same as this 3.1 does in fact i can even roll back to 3.0 and i product and they probably will just you can see it and work with it in the production version not the pre-production version so hit create and once that gets created notice is downloading the the project information or it will be let's go over to our mesh here we'll edit our project and change this at 3.0 and then do a rebuild to make sure that we'll build we have a failure alright and says that the obj folder there's a project assets json that doesn't have the right version no problem we're going to open a folder and file explorer up there it is open another screen we're going to delete the bin and obj folders those are folders you do not need they get recreated for you when you rebuilt now when I do a rebuild it should be the restores of correct versions and it builds succeed which it does so we're all set now we have a 300 project so even if you did have 3.1 that would work but he could also run it on a production machine that has 3.0 so no worries there if you have don't have the preview version like I do so this is a basic asp.net razor pages web application and in it we have our index that CS HTML dot CS project which is pulling a logger and it can do some quick logging on this so we could say logger dot log information and say you requested a the index page ok real quick it's already baked in using the the I logger inside of asp.net core now I logger is an interface which means we can plug any type of concrete value against that as long as it implements the ilogger interface so right now it's using the built in logger but we can change that to use sere log in just a minute but first let's change this to say Sarah log demo which what this does is it starts whats called Kestrel and Kestrel is a web server that allows us to see the command line up what's actually going on with our web application it's going to start that I'm gonna pull it over here this is our Kestrel and if we scroll up here to be kind of hidden me there's still an info event but in here should be our request where it says hey you requested the index page and it's in here if so you can find it right there so info and it says you requested the index page there's our logging event now as all these other log events are happening as well well these events are ones that Microsoft has put logging in their stuff so you can see what's happening around your application so for example the page action invoker where the value of for inside not sure that 4 stands for execute the page index in a hundred and one point two four zero four milliseconds so a tenth of a second it loaded the index page alright and there's a whole bunch more information here as far as getting information for JavaScript and a bunch more so this is all the stuff that's going on this is a very very helpful window to be viewing instead of the is this castro window allows you to see what's happening on your web server so for example i'm a click office actually bring on the screen but watch this this window when I bring this window over here and click on privacy okay hey nothing's happened awesome I think the reason why is if I have there you go I had hit enter so I'd pause a screen because I had been clicking on it but now if we go back to privacy again notice it now goes to the privacy page and it shows more information about how long it took the return message which was 200 meaning success and so on so you can see every time you get an action it shows you what what the days go on behind the scenes make me very helpful that's part of the logging system now let's add a little more logging this is not necessarily a here logging is and.net core 3 I have a video on that and I'll link that in the description probably but the this is not a video on just logging it's about using Sarah log with our longing so let's set up a few things in here just to make a little more logging information for us it's going to a try here to catch an exception and in this catch what I'm gonna do is I'm going to do a logger dot log error or you pass in the exception and they pass in a message saying a we caught ght there we go caught this exception in the index get call ok and that's just going to catch an exception we had to throw an exception first so let's do this let's do I just do a throw throw new exception this is our demo exception so you know coming into the on get it's going to log something and then immediately it's going to do a try-catch and in the try it's gonna throw an exception which amigos the catch catches the exception and then logs that exception is this a standard thing to do course not you don't just throw exceptions just to catch them but what this will do is will give us an actual exception to put into our logger in fact let's make it look better by I meant cut this out temporarily and I'm gonna do a for loop where I is less than 100 so start from 0 and loop up and we're gonna say yeah if I equals 56 so 56 when I is 56 then throws exception otherwise what we're gonna do Israel log for us a logger dot log info and we'll say the information is let's do this the value of I is and we're I put our our value here okay like so in fact I'm gonna call this the I variable alright now one of the things that that I have heard before is ot mean if you made a mistake you need to put the dollar sign first and that's incorrect you do not put a dollar sign here these curly braces are different than a string interpolation okay strand Terp relationally put the dollar sign first and then you put a variable in here instead this looks a little bit more like the string dot format or what you do in a a console.writeline where you have the curly braces here and that refers to a variable after the fact okay this is where we get into structured event logging so what happens here in a simple logger is it's going to take this value of I and place it into the first variable in line this variable now I could have called this I that I initially did but I call I variable for a very important reason I want you to realize that this name does not correspond to this name it does not matter what these two are named it's based upon position and the reason that is valuable is because you can have something called I but really what this is is a loop count value and you call it loop count value in this case I call I variable I could call it loop count variable or value okay and it's still gonna work just as fine alright but what this does when structured event logging is used is it will it will save this string the output string which is going to put one two three four in this spot but it will also save separately this as the variable name and then the value as the actual variable value it's gonna save that separately so we can pull it out it can do searches based upon that we can do filtering based upon that and all the rest so very important these structured event logs but right now we're not using structured event logs we're using the basic event logs that come with our application or with inside of asp.net core so it's runs again and we're gonna see what happens in our structured event okay so it just happened we scroll up we got a our one through 55 so the value of I is 53 54 55 56 it throws an exception we caught this exception in the index call we got a fail message okay now we didn't continue on after that but says I put the catch outside of the for each we can change that he want I'm not going to really adjust like that so they'll log 0 through 55 and then it's gonna fail on number 56 and catch that exception okay so that's our our setup for applications all it's gonna do now let's look at logging let's start by going to the program CS file and this program CS file let's first of all fix this I like using the block body for this it does make a formatting mess but using the block by allows you a little bit nicer and cleaner I think but it's up to you if you want that format either way it's just putting the create braces around it's all it's doing versus the arrow function okay so we have this static void main this is the entry point for our web application but what I want to do is add logging right from the start so I am going to set up Sarah log right here the first I'm going to do is a guide talk to the configuration file the app config file or the app settings dot JSON file which right now has some settings for the built-in logger which I'm going to get rid of in just a minute but first I want to pull settings from this for Sarah log because you may have seen Sarah log being configured right in code and that's that's fine for quick and dirty get get it working what you really want to do is you want to configure an app settings JSON because then you can change it at runtime so I'm gonna do first is I'm going to save our configuration equals new configuration builder not binder builder then I say add JSON file and the JSON file is app settings dot JSON and then I say built and what this will do is it going to build a configuration system so it's AI configuration root this is because we don't yet have dependency injection at this point in our application that comes later and so we can't just ask for an eye configuration value we have to essentially do what the the system is doing behind the scenes later we have to do it now and that's asked for configuration now I am just asking for app settings JSON I am NOT doing key vault I am not doing environmental variables I'm not doing the app settings dot development JSON file I'm not doing any of those things just the one file and that's okay and the reason why is because this apps thing is that JSON file is where I will store everything all the time even for production normally you have all those different options because at production or in staging your or your test environment you're going to want those in different places so for example a connection string you're going to want to have in app settings that JSON for local testing but then for production you won't want to have it an app setting JSON you'll probably want it in either the environmental variables in let's say asher or you'll want in your a jerky vault in order to keep it secure and not accessible and see why over at the settings for logging we're not going to go that route we're just going to store everything here if you want to change it at some point later you could do that but I think this is plenty for what we need it's not the same as secured passwords okay this is just settings for your logging so it sets up and says okay you can now talk to the configuration builder that's step one now we're gonna do next is raius a log dot logger equals new logger configure now right now - yelling mean saying I don't know where this stuff is and if we do a drop down here I don't not insure the control dot we'll find anything oh it does install package serial log okay so it can install a package seer log for us we're gonna do that manually so let's go to our dependencies right click and say manage NuGet packages River browse and we're gonna search for Sarah log dot asp net core so it's the serial log for asp.net core logging now if you were doing a hosted service like a worker service you might choose the hosted services extension instead but this worked just fine you can also just install just Sarah log which is a base but this got a few more features for us and it does have a dependency on Sarah logs what's kind of install that anyway all right so we'll install that and accept and now we can come back to our program CS and if we do a controlled diet if they have the ad using Sarah log at the top alright now that we've done that we can say dot read from and we need another extension for this so there is now a way a read from our configuration file until we go back door to pen so you might wanna keep us open because we're a couple times more but do want to show what each part did all right so Sarah log dot Settings dot configuration so it's going to read from our app settings JSON file so Sarah log sayings dot configuration I'll leave it open and now we can say read from dot configuration oops you know what that shouldn't be that should be logger config raishin there we go now you do the pass in a configuration here which is passed in this variable right here so dot build oops not top build de hunt create logger there we go I was reading the top of this lamp here so you got this longer now setup so this part needs that sare logged odds asp net core that's what it needs then this right here needs the sarah log dot settings dot configuration package and once you do those two things now we're reading from the configuration file the app settings dot JSON for our sarah log settings now if you see example online you may see instead of this read from you may see different settings actually set up the different loggers so for example you might see something like right - Dodds file and that's got some configuration in that file ok that's pretty typical for the examples however that's configuring your logger right in code we're not gonna do that today we're going to configure it all in our app settings JSON so the only configuration we have for our logger is going to be from the app settings JSON file that's it okay now before we leave this I do want to wrap this so actually going to do a control X here just easier you can do surround with I found that it's just not quite as easy as I want to be so we're going to first have a try/catch or I have our line pasted back in here and we'll do a log dot information here that says application starting up that's me a log message that says our application starting out pretty obvious however I want you to note that since we're in static void main we just used this static keyword log and we called logged on information not log information so it's a little different me that's not using ilogger right now it's using the Sarah log logger which when we call for an ilogger later it will be the sarah log logger but it will be in that interface and so will users that interface like normal but before we get to the interface because remember don't have dependency injection yet we can just call log dot and as information error and all the rest so we'll start by saying hey our application starting up and then actually start our application this catch here is just in case something bad were to happen in our application whereas a log dot and we'll go a full fatal fatal normally reserved for if the application totally bombs if it's done if it crashes unhandled exceptions those type of things well if you can't even start your application that's a fatal exception so pass in our exception and say the application failed to even start or fail to start correctly how about that okay so you have a try-catch the one last thing you want to add here is a finally never finally we'll try catch finally whether we go through the try successfully or not where we go we hit a catch or not either way in both of those scenarios we're going to run the finally code no matter what so this is kind of cleanup code alright so you what make sure that you have this finally because we want to have the log dot close and flush and what this does is if you have any log messages that are still pending they haven't written yet this will make sure that it pushes those make sure they're written then closes the log all right so it's gonna close it correctly so that's now all in our static void main we scroll down a little bit into our create house builder down here right after create default builder we want to have one more thing so dot use sera log and what they'll do is they'll use sera log as the logger of choice rather than the builds in dotnet core logging system okay let's go over to our app settings JSON and in here I remove this logging section because this is for Microsoft's logging but before I do let's go back one second notice that the default is information meaning information logs or entries are higher so information warning error fatal as I think a couple more in there but those an above we get logs I think it's debug that won't get logged when you have information level okay so there's that hierarchy that you can look up but the hierarchy says in this case start the information above nothing lower than that but there's exceptions for Microsoft and for Microsoft hosting that lifetime for Microsoft it says warning and then from Microsoft I host in that lifetime it says information what that's doing is saying the most specific rule wins so if a namespace where the the log message is coming from is starts at Microsoft it has a a warning level or higher in order to get locked but there's an exception to that if the namespace is Microsoft out hosting out lifetime value information again okay so is some filtering going on here to say based upon where it's coming from what kind of message do we want to log everything or some things normally want to log at the warning level or higher but I just want to kind of point that out before we delete this because we're going to add our own filtering system and it's a little bit different okay so now our app settings JSON has just allow hosts and everything after this I'm going to put a comma and then I put in quotes sarah lock okay and then it puts my curly braces so this is a and a JSON file so it's a JSON format which means essentially it's pretty similar to C sharp as far as curly braces have to match square brackets indicate an array of objects whereas curly braces indicate an object right so we're going to start off by having an object which is the clear braces and then a bunch of properties all right so the first property I'm going to say I may add this here but not use it quite yet and that's the using statement now just like in c-sharp we can have using statements there's an array of them inside of our code I won't put any yet but this array will allow you to say you know instead of saying you know system dot text dot for something you can put using system dot text in this message box and then it's used whatever was after that alright so just like hidden c-sharp code it makes your code a little shorter later on okay so for now we don't quite add any but I want to put that there so you know how to put them in later if you need to minimum level it's a minimal level for logging and I say the default is going to be information just before but I'm gonna override so here my overrides my first over eyes me from Microsoft so anything in the Microsoft namespace is going to be warning I'm also going to say anything in the system namespace is also warning all right so now I've said by default we're gonna log information level or higher but for anything the Microsoft or the system namespaces they have two warning level or higher all right which means I've now essentially turned off all the default logging so all those messages we saw we had it kind of scroll through a ton of them those are getting turned off these are not warnings or informational now those information messages do help us in diagnosing a problem but we don't want to log them all the time because the fact that they're just it makes it hard to find our error messages or our log messages so how to turn them off by default it is the warning or higher later if you want to turn them back on for debugging you could do that right here just change its warning to information all right so after after minimum level they were going to have in a rich and what this does is this is where the structured login comes in we're at enrich our messages to structured logging with additional information I'm gonna say the first one is from log context and next I'm going to say with machine name something's are pretty obvious with process ID and with thread ID so essentially these are going to give you additional pieces of information about what happened when the log message was called this will allow us to diagnose problems easier because we have again the context of what was going on when that happened so you have a thread ID you have a process ID you have the machine name these you can line up with the information in an event log maybe you had an event on your computer it was unrelated you thought to your your your app Asian but it has the same process ID you can match those up and other things as well so as this information can be very helpful but out of the box these enrichers do not come with Sarah log we have to add them so let's go back to our nougat feed and rest say Sarah log dot e n RI CH e RS enrichers I just look like that we have multiple environment thread process context so I grab environment and install that I'm gonna grab thread install that and a grab process install that notice that these three all come from Sarah log contributors the next one I'll list comes from a person these from people so these are additional ones that people have added but these first three come from the Sarah log community as a whole these are more official I guess you could say enrichers okay so we're add these three enrichers and that will give us all four of these options for enrichment okay we'll put a comma at the end of this and then we're going to say right to and remember before in our program CS I said that normally instead of this read from you have the dot right to instead of doing here in our code we're gonna do it right here right - alright let's get a Rea so we're gonna have multiple objects in here so square brackets inside here gonna say the first one will be really simple just do console so whoops have my fingers in the wrong keys name is going to be console alright and that's it so this will use Sarah log and right to the console now I believe consoles already installed as a sink so whenever you write to something in Sarah log it's called a sink so you have the file sink you the console sink and there's a bunch of other sinks as well this one's a console sink it allows us to print out to the console I don't think we need to actually add explicitly the console sink because I believe if we look at the Sarah log itself I believe sinks we have file and we have console as dependencies with this Sarah logged asp net core so you'll need to add them explicitly because they're already implicitly added ok so that's all we're going to do for our configuration we've configured it to write to the console that's it for now we're going to enrich it which the console is not a structured sink so it's just going to have basically the messages but not all the extra information so this is not really useful yet but we'll get there now the one last thing I want to do a for you start this up is we do want to turn back on the whenever it calls a page so ever a page is called we want to see that right now since we turned off everything from Microsoft and system that's not going to happen because they're not warning messages they're informational messages but if we go a startup dot CS and we say add a user see them up here so using Sarah log and then down here after our use static files so before you outing I say app dot use Sarah log request logging well that's gonna do is whenever a request is made Sarah log is going to log that ok so even though it's not normally get ignored we're gonna make sure you log that in Sarah log all right let's start again like anybody is in Kestrel it's gonna look different this time those four so it's white and there's a lot less information on the screen first of all we have the application starting up that came from our program dot CS right here all right and then we have the you request of the index page so that came from our our index page right here and then next we're gonna have all of our log informations until our exception which we do and notice the the color of the number is different that's because it's actually a variable got replaced okay so go through until you get the 56 but 56 instead we get an error which notice that the e RR is in red instead of INF for information and it says we caught this exception the index call and it has the system died exception this is our dental exception where it was line 33 and then at the end we get this message that says HTT bit get call for just slash okay that's what index is is just slash respond to the 200 meaning success in 416 milliseconds notice those are in that purple color because those were also injected values which means if I go to the privacy page notice that it said how long it took and it says a slash privacy so now these values can get searched on later if we were using structured logging which were not yet we're gonna get there okay but this is now what our logger looks like using Sarah log it's different but similar okay now even if you're using a structure logger I would encourage you to also use console whenever you're doing debugging at least because console is really easy to look at and say okay there's our problem right there so that one took 416 milliseconds versus 23 milliseconds which by the way that 416 milliseconds the majority of that time is taken up by throwing the exception okay throwing and catching the exception so throwing catching exceptions is expensive okay so let's let's turn this off and now let's go back to our app settings JSON we're going to do is add another couple of syncs remember that this is a square bracket which means it's an array which means I can add a comma after our console and for this one I'm gonna say name is going to be file and this is a keyword console and file these are the sync names all right I'm gonna say arcs and so the arc it's gonna be a object and the first arc is path all lowercase and I say the path is going to be D : / oops double slash demos double slash logs double slash log txt okay so it's going to log to my D demos logs folder now a double slash is because the single slash is an escape character so you do it twice and it escapes a slash and puts just one slash in its place alright so that's the path and then the outputs template and this is where you get to choose what do you want your template to look like and let's start with curly braces here time stamp and we'll say if the G time stamp which I believe is UTC and then space and we'll say message and we'll say new line new line : one which means it's optional exception one and that's it so it's going to write out a little bit more information to our log file but still just one line of text all right so there's our second sink let's do this so right now let's open up that folder notice it's empty entirely so d demo logs is empty entirely now let's start this up all that start and get away through and I'm gonna shut it down and open up a notice log txt and there's our log file it looks a lot like the console all right there's not much different in fact it's pretty much the same thing except for the fact that it can't highlight these these are just text values not easy to search on so if I want to say give me a range of the values of I they had a problem it can't do that give me the average time it took to load a page you can't do that in here because these are just text values I'd have to do some kind of parsing in here to say okay where it says in after saying get an after it says just and slash then after in about everything until it says MS pull that out convert it to a a double and then do the is just get a mess okay that's such a fancy way of doing things so what we do well this is where we get into structured logging let's first do structured logging to a file I put comma here and and do another section the name is going to also be file okay and the args is going to be first the path let's do the same path here in fact let's do let's just copy this whole thing and paste it in but this time I'm gonna say log instead txt I say log JSON okay but that doesn't do it quite yet next I want to say formatter Sarah log formatting dot JSON JSON format er comma Sarah log now by now you've probably started to wonder well Tim where do you get all these settings if you go to Sarah logs documentation it has documentation for every single sink it's not entirely complete but it's pretty close to entirely complete I mean they've got a lot of stuff and so you might as I hunt and peck a little bit I try to pull up the ones that you most likely use and at least show you how to do them that way it makes your life a little easier but that's the documentation for Sarah log is pretty impressive right so we're gonna run this again this time we now have a console we have a text file and we have a JSON file I'm gonna run this I did not delete my old log file okay so it's still there it closed this back out open us up let's start with our text file and there's our first log information and we start again and there is the next set of logging information okay now log JSON this is a little harder to look at if you look at through say notepad but I'm gonna open up gonna do a git bash erease it's just easiest and do code space dot this is gonna launch vs code F this folder there's makes a little easier and one of the options in here is I can click on the log JSON and expands it out and it makes it a little easier but then if I right click on here and say format document makes it even easier now this is not in theory it's not a properly formatted JSON file and the reason why is because this is an object and let's expand it out a little bit this is an object but notice there's no comma after the object that's because every line is treated as one entry not as multiple entries in the same overall object so you know these are independent independent objects they are not in any way connect to each other but this is structure logging so the first one is level is information time-stamped notice these are all separate so the timestamp the message template says applications starting up and then properties machine name process ID thread ID okay especially that machine name can be really really helpful go down one more level and you have even more information you requested the index page but notice all the information we're pulling out about that what the action name was what the process ID was parent which isn't how parent right now okay let's scroll down to where you find our exception all right so there's our exception and let's let's do this make it a little larger so our exception here we have our timestamp we have it's an error the message template we caught this exception in the index get call and here is the exception and then the properties for it so it's a little bit more information even than that all right also notice right above that message template the this is one of those successful ones the value of I is loop count value which seems a little odd that is not really helpful but loop count value is 55 so it it's not storing 55 in this string this is the templates this is the value which is a separate property unto its own all right so really valuable being able to pull these pieces apart now let's make it even better because a text file JSON file is not really what we're looking for alright it's helpful it allows us to then bring it in a c-sharp or you want to impress it but it's still not immediately usable so let's use another event sink besides this file console remember get another one in that's more helpful we're gonna bring in is a tool called seek or seq I'm going to rent the web page here this is seq or seek and what does it gives us that structured logging and it gives it to us in a web page format we can even see charts and graphs of our application alright so some pretty impressive stuff that it can do for our logging right in a box now I do want to point out that this is not free if you are using more than one person on the team that's using this okay so you have a team at a job you're gonna have to pay for it but if you're an individual developer you don't have to pay for it there's also I believe it's free for open source as well so even though the pricing can be somewhat expensive this is professional for a full year support but it's yours to keep using after that that's for a year for up to 15 users but free for development and single user deployments alright so you can have this and use this for free so I don't often show paid products but this is one of those cases where if as you're learning it and as you are starting to work with it it's free it's only when you get onto a team and you want to use in a team environment that you start to pay money for it alright and again you don't have to use this I've shown you how to use the console I've shown you how to use even JSON files so you can have your own parser but visualizations like this are pretty powerful for showing off what's going on with your application especially a larger application that's pulling in lots of different events so we're gonna wire this up a may say that sounds intimidating it's really not so especially for development purposes let's do this right oh we already have the new get packages open and we defined a new new you package of course Sarah log Dodd's syncs dot seq alright so this is it the seq sync for Sarah lock install this I mean you configure our apps things dot JSON and it can either leave these in or turn them off gonna leave them in because that way you have them if you download the source code so comma after this and we're going to add another object here for another right to in this case the name is going to be seq again that is a I the name you have to use you can't use a different name that's a keyword and then the args server URL if you have a server okay what's our server URL well I'm gonna choose HTTP localhost and the port is going to be let's do 80 80 81 alright I don't think I have anything on that port currently but now I just demonstrate the idea that we have a server somewhere that's running seq so we have to be installed and there is instructions on how to install it but I'm not gonna install a server a logging server on my computer that's gonna be complicated and take you know resources they have to uninstall it later and all this stuff instead what I'm gonna do I'm gonna use docker now you don't have to use docker if you go back to the documentation there is a documentation in here I believe you go documentation is going to have how to install it yep I install it with Windows and so here are the steps for installing you do the next next next kind of thing and say it up we're not going to do that we're gonna use instead docker because docker allows me us an instance of this in Linux of all things on Windows but in Linux that will do the logging for me and then it's not gonna touch anything on my machine but it's treating it as if it's a real live application which it is ok so we're gonna learn how to configure docker it may sound like that's intimidating this is actually really easy if you have docker installed ok now if you don't have docker installed I believe it is hug docker calm and then you sign in and there's me a icon down here to say install docker desktop do that and then what you're gonna get once you install it is a bunch of settings that look like this ok so this is already installed make sure it's running me as login and check for updates another shared drive make sure that you share your drive so you can have access data from your hard drive into a container they'll be important because you want to save the logging data outside of our docker container and the reason why is because I'm going to turn my docker container off and delete it when I'm not using it and yet when I want bring it back up I want to have all my log files that already happened so I'll show you that in just a minute alright those the big ones I do have it set so that's operating there's some configuration here I've got set here for running Linux even though I'm on Windows I'm running Linux containers ok so that's the other configuration bit all right now if docker is of somewhat concern to you not sure what docker is I do have an introduction running in no time the reason why docker is so important is because it makes tasks like this like split up a web server with a logging system on it trivial alright so I'm going to do is I'm gonna open up power show some powershell and doesn't matter where I am so if I really do docker images you see I have a couple of images here but not many all right so I'm gonna say docker run now docker run is gonna run an image so you already have an image built and it's going to run it now we haven't built an image because seek did it for us they built an image for us that is their server it's all I have to do is say run - d4 detached mode let's make this a little bit bigger let's change this to be 28 and see that ok a little better so - d4 detached mode and I'm going to say double - restart space unless stopped so detached mode means I don't have to keep this window open in order to keep my container running the - - restart unless stopped indicates that my container will if there's a it's a problem it's a I reboot or if something crashes its gonna restart that container always gonna try and restart that that logging seek container unless iris a explicitly stopped this container okay next make it have a name I'm gonna say seek is the name seq - II for an environmental variable I'm going to say all caps except underscore EU la equals y that's going to accept the end-user License Agreement and say yes we agree to the terms of the seek server - V what this is gonna do is set a volume that's going to allow our system to talk to our local machine the importance here is I want to save my logging files on my hard drive not on the container hard drive because the container might go away in fact it will go away at some point I want to keep my logs that way if I split up a new container later I can point to the same location and have my logs still so we're going to point to location the location is going to be D : / demos / logs if you remember that's this location right here we already have two log files that's for our application log files but we'll put also our log files for the seek server okay same location so logs NSA : slash data so what that will do is the slash data folder inside the seek container it's gonna actually be my D demo logs folder on my hard drive alright so as a map that container over or map that folder over so whenever it stores data in the data folder in the container it's actually stored on my hard drive instead alright then I have the port so - P I'm gonna say that the port I want is 80 81 that's the poor I'm gonna use on my computer that's gonna map the port 80 on the seek container okay and finally with no other parameters here when I say data lust so dat ALUs t / seek : latest and that is the that's the image i wanna pull so data lost / seek so gonna resize it a little bit there you go so data lost / seek : latest so we will pull the latest image of that seek server okay so it's going to run detach mode it's going to restart and less stopped its gonna name this container seek its gonna accept the end-user license agreement it's going to map the data folder internally to be these D demos log file on my computer and it's gonna map the internal port 80 to be my computer's port 8081 okay I hit run and says hey I can't find that image locally I'm gonna pull it down from the cloud so it's currently pulling that image down the image is a little large not massive but it's large enough that it's gonna take a little bit of download it's downloaded and now it's running okay which also means I move over here notice the D demo logs folder now has seek JSON and it has the logs folder extends folder and Documents folder this image is now running I can close this out if I were to open up visual studio code and go to my docker it has one container up okay you can see the container here you can see the image that downloaded so we're up and running if I go to my browser and I were to go localhost colon 8080 one there is my seek server up and running you see how easy it was it was just I needed to run that one docker command and it's up and right now there's nothing in here yet because we haven't logged anything yet let's change that we're pointing to already we would install the package let's run this okay so it's going to launch their console give it a second you might get us error once in a while to prevent unsafe abort when when evaluating the function system dot run time downloaded assembly load context yada-yada all threads were allowed to run this is a area you'll only get in development when you're trying to minor things and that's okay you can okay it will go and now our web server is started we can hit stop we can come over here to the seek server and hit refresh check us out we got a bunch of stuff in our server including that error click on it there's all the information about the error they click on this the value of I is 55 notice let's zoom in here a little bit the value of I is 55 that's the actual message we were to see in the console and yet if you hover over it says loop count value come down here we can see loop count value is 55 so this is going to show you more information about the requests the really cool thing is and let's zoom out out a little bit is if you go over here to dashboard in the upper right hand corner click on dashboards and go over view we can now see all the errors in the last five minutes every five seconds we can refresh this every 10 seconds let's say so it shows the errors and verses the information so 59 informational messages which is 83 point or a nine ninety eight point three percent and we only have one error if I were to come over let's let's start this again and we'll bring us back up while it's starting and it's gonna pop up an error here in just a minute the Refresh will hit and it's going to show you have a second error there we go so second error popped up so this is now a live look at your dashboard you can add more things here if you want like the the different type of calls you can query on things it's a lot of information you can pull off of these dashboards you can add even more to them so this is the power of structured events so a structured event gives you a whole bunch of information about the event that you can query on you can pull the values of those those things you insert it out so loop count value of 35 you can get more information about an exception so this first of all it turned Reds you can see it and then there's information and pull out of here including the full stack trace of that exception we can also say only give us certain things they only give us our errors well there's our errors we have filtered out all the ones that aren't errors and now it's just these two you can then do some work on those and see what the errors are and figure out you know why they happened all right so there's a lot of information you can pull out of this and it's a great way just have something up and running very quickly that will monitor your application and and give you information on what's going on the problem sometimes with with the text files whether it's a JSON file or a dot txt or the console is it's very easy to miss stuff and you don't necessarily pop in there and look at it and sort through everything because it's kind of hard to look at with this it gives you a really clear indication oh there's my errors or you filter on them or you go to your dashboard and see them pop up and then do some investigation based upon that so I click that there and says there's the exception for that time point so again there's a query that it ran to find those so some really very very valuable pieces of information you can pull on this and it gives you good visibility into your errors and logging and that's the most important thing because if you're just logging stuff you're not doing anything with it why you logging longing has to have a purpose and one of the purposes is to make your application better by exposing any errors that you might not have otherwise known were happening and the way to easily expose that is to have some type of logging system that includes a dashboard like this where you can keep an eye on things and go oh we had an issue you know and this is by way there's five minutes you can actually say the last 24 hours so if you want to have a you know broad overview but it will allow you to dive in say oK we've got an Asian or application we saw a spike in errors let's drill in find the issue and then look at those exceptions and pull the information out and figure out what's going on that's what logging is for to help those type of things or to give you a flow so that if you're trying to figure out how do users use our application you can come over to events and you could filter based upon the person or the machine so let's say that I am usually l1 user users one machine you could say let's filter only on this machine here and then say okay what did this person do from the time I logged in to the time I logged out then you can track their movements using log statements and understand that oh they're skipping around here we could put a link that make their job easier or something of that nature so logging can be very powerful as long as you use it if you don't use it why bother and that's why it's also nice to be able to have the values in the app settings JSON because you can come up here and say you know what we're gonna change this to warning and not log anything else because we're not using anything else we're just using warning and higher and so why clog the server up with a whole bunch of files we don't need so you can do that all right but then when you have an issue where you say okay I need more context around that warning you can change it back to information and then run it on that machine we're running everywhere but then capture just that machine and say okay what's the context around this error what were they doing that led up to this issue so logging is very important can be very very powerful if you use it right Sarah log makes it even more powerful than the default logging to a text file or logging to even a database be this structured logging allows you to get some more context and do some filtering based upon those different variables so structured logging is a really powerful addition to dotnet core and Sarah log takes advantage of it really well with structured logging for Sarah log the easiest thing to do is probably the adjacent file so that's this right here that was pretty easy to do but it's a little harder to read and if you have the ability to either run docker container or install the web server for seek then this is a great option for getting visibility into your application and people to use those structured events to their greatest extent okay so that's a video on Sarah log and then how do you seek with Sarah log I hope you've found some value in that I've hoped that you can see how to make your applications even better and be able to really dive into what's going on behind the scenes of your application if you have any questions I'd love to hear them so write down the comments below or other thoughts as well and how you can make Sarah log even better or make another option besides Sarah log like and log or something else okay either way I'd love to hear from you thanks for watching as always I am Tim quarry [Music] you
Info
Channel: IAmTimCorey
Views: 102,052
Rating: undefined out of 5
Keywords: serilog with seq, c# serilog, c# seq, seq, serilog, tim corey, i am tim corey, learn c#, learn c# the easy way, logging in c#, .net core, logging in .net core, asp.net core logging, asp.net core, serilog with asp.net core
Id: _iryZxv8Rxw
Channel Id: undefined
Length: 67min 56sec (4076 seconds)
Published: Mon Nov 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.