How To Create a Simple Log Writer in C# with also using the App Config File - C# Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everyone so while back someone asked that i show some more beginner-friendly c-sharp content and i'm perfectly cool with that uh hopefully you guys are too i think anyone can benefit from this even me if i have to brush up on some stuff that i typically wouldn't work on or work with it's still good practice so anyway if you like this kind of content don't forget to hit subscribe and today we're going to look at creating a logger in c-sharp in our well we're going to do it in the console application but you can actually do it with a bunch of different applications and you might be asking why would i want to log maybe that is a is a new question to you and my answer to that would be a lot of the times we have applications that run when we're not watching it actually majority of the time they run when we're not watching it maybe it's a a web app that's running 24 7 right or maybe it's a console app that's being called by a scheduled task and therefore we wouldn't always have eyes on it to see if something goes wrong and then inevitably something is probably going to go wrong so what you can do is you can reference the log and you can see okay well what failed or what happened to cause this to uh to fail and i'm sure you can see how that can be useful right when you're trying to debug a production application so i just want to show you how to make a nice little class in c sharp that you can call and you can write to a log file so what we're going to do for this demo is i'm going to create a nice little console application and we're going to create that class so i have visual studio pulled up here let's go ahead and create a new project and then i'm going to choose console app.net framework just for this example and let's go ahead and name it uh logger demo okay so create that and here it is so the first thing i like to do with any app is to just test it out make sure everything works it should right but you know it's more of my peace of mind so i'm just going to do a console write hello and then we can do a console read so we can actually see it before it disappears so let's start that just make sure this compiles okay hello good so you can see that looks okay hopefully this is zoomed in enough for you guys and now i'm going to create a class that we can call a method inside of that class to write a log to our log file so i'm going to whoops i clicked on the wrong thing i'm going to right click on our project in our case this logger demo here hover over add and then at the bottom you'll see class so we'll click on class and we're going to add a new class and i'm just going to call it like logger now hit add and here's our new class what i want to do is i want to change this to public static and the reason i'm changing is static is because i want it to not be instantiated every time i want to use i just want to do logger dot whatever we name our method and call it without having to instantiate it first and create an object okay so before we get to creating the method something i want to do and i think's important for you to do as well and if you don't know about this know about it is our app config or you'll have web.config if it's a web app well it's a configuration file right it has all of our different configuration settings that we can change without having to recompile the program in our case i like to keep the path of our log file where we want to write this log file as a configurable variable so what i'm going to do is in this markup i'm going to create a tag and our add a tag i'm not creating it adding the app settings open and close the tags right here and in this you can have key value pairs so the key is going to be like log path and the value is going to be i'm just going to put it in like c and i made a folder actually let me show you on my c drive called temp and it's blank right now so c temp and then we're going to name it log.txt but you can you know put it wherever you want you can put it in the desktop or in your documents wherever you think is appropriate actually i think i put this in the wrong place we need to put this in configuration right here there we go it looks better so right now i'm putting in c temp log dot txt is the name of the log file that's going to be created and written to and what's nice about this like i mentioned you can change this without having to recompile whenever you want to change the path where it writes to so let's go back to logger.cs here i'm going to make a public static void and this is a method i'm going to call write log and it's going to take in a string and we're going to name it message okay so in here i'm just going to create a little variable called log path and this is where we're going to reference our app config to get the log path and to do that we need to do oh well actually let me just uh configuration manager i think i spelled that right but we should get a red squiggly and then i'll hit control period and it'll tell me how to fix it and we want this using system.configuration and now it turns green it means it recognizes it now we can do app settings and then it's kind of like a list where we can give it an index of what in the app settings do we want and we pass in and the index is let's go back to app config the index is log path that's the key that we gave it so put that there all right and that'll take the value of log path in app.config so here let's go ahead and start writing to our log file so i'm going to do using streamwriter and the reason i'm using the using statement is because i want it to automatically delete streamwriter after we're done streamwriter and i'll just call it writer and you can see we need to bring in a new namespace so using system.io let's do that that's good and then equals new streamwriter and it's going to take in some parameters it's going to take in the location of where we're writing to so it's going to be log path and then the second one is the one we want string path and then bool append we want to append to the log file if we didn't it would just clear out the log file and then write the new statement every time and we don't want to do that we want to append to what's already existing then here we do writer dot write line and then message we just write to it the message that we get that's what you could do however i have a different method of how i like my log files to look i like to give it a time stamp so i can get an idea of when this message was written and when the error occurred when i look at the logs so what i'll do is i'll do some neat little string interpolation and here the first thing i'll put is date time datetime.now and that'll put it in our log message as the very first part of the message so we can see okay when did this error occur and then inside of this output message and now we can get rid of that all right so that's all we need to do i'm pretty sure this is all good now i don't know why this opened up i'm pretty sure this is all good now we can actually go back to program.cs and i could show you pretty easily how we can use this so let's say well first let's just let's just call it so we're going to do logger.rightlog and then we're going to pass in a string that's going to be our message and it's going to say whoa there was an error okay so let's run that and it's going to be real quick because it's not doing anything else it's not going to display anything and now if we go here you can see there's just a log.txt so let's open that and you can see there's a timestamp and there's our message and now the theory is if we run it again and we open this up it'll have another time stamp and it'll append on to the existing log file which is what we want we don't want it to erase it every time it runs because that wouldn't help us out all right so i'm going to close this and keep this open so what you would do in an actual application is you would have a try catch block right and if you don't know how those work i'll actually probably do a video on that next so i have a try catch and then exception ex this will catch any exception and you would put your logic and whatever whatever is going to execute inside this try block and if an exception were to occur if something were to go wrong it would then catch it and then what we would do is we do logger dot rate log ex our exception that we caught and a message itsmessage is what we'd write to the log file so you'd put something here let's say somehow it compiled and you tried divide by zero it made an exception and then it wrote that to that actually let's try it let's see if it works so let's do int a is equal to three divided by zero i don't know if it'll let me compile or not let's try it i think it did so let's go back no i don't think it did let me compile i see a red yeah division by constant zero all right but if it were to let you do this um it would then catch the exception right to our log what what happened so anyway guys thanks for watching hopefully you found this useful and i will see you in the next video
Info
Channel: Coding Under Pressure
Views: 43,626
Rating: undefined out of 5
Keywords: How to write to a log c#, C# logfile tutorial, create a class to write a log c#, c# for beginners, c# beginners tutorial, c# beginner project, logfile, write to a log
Id: DrXIWwBfUWI
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Mon Mar 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.