In this video we're going to look at how to log data.
And the idea behind that is, is just to show you how that works, because I want to use that code in
the next video.
And in the next video, we're going to go over how decoupling works, or at least to give you a general
idea of what decoupling is and an actual example, and also go over why and when to use it.
So there is this file class, right.
And the file class has a bunch of really cool methods.
And this is a class inside of systems.io.
So IO stands for input output.
And this one allows us to create copy delete move and open files.
Okay.
So that's pretty cool.
And the file here now will do the following for us.
It will append all text.
Now we can also append all text asynchronously.
But that would go a little too far because we would have to use await and so forth, which Yannick is
going to go a lot deeper into once we go to multithreading okay.
So we're going to keep it simple and we're going to say append all text.
What this will do is it will append all the text that we passed to it to the path.
First we need to decide which path we want to use.
And I'm just going to keep it really simple.
I'm just going to say okay, inside of the context or inside of where you are, please create a log
text file if it doesn't exist and if it already does exist, just overwrite that and append the text
that I am writing now.
So I'm going to say something like hello World for example.
Okay, so this line of code will create a file and we'll write the text hello World into it.
The file name will be log dot text.
So let's quickly run this and we see nothing happens right.
Or at least we don't see anything on the console.
But that is fine because we don't care about the console.
What we do care about is what happens inside of our interface app.
So inside of the app you have debug dot net eight.
And there you have this log file which was just created at eight 4054, which is just the time now.
And if you click into it you will see it says hello World.
Now let's run our program once again and you will see that it will add Hello World just next to it.
Okay, let's run it.
Close it open log again.
You see hello world, hello world.
So what we obviously can do is we can add a backslash n here plus and backslash n like.
So this will just create a line break.
So if we run it again our third Hello World will be in the next line okay.
So let's open up this log again and actually didn't see.
And if we run it again we will see.
So run it close it.
And we should see that the next hello World goes into the next line.
It didn't.
Let's run it again and close it.
And actually the log edit text was still open.
But yeah now it actually put it into the next line.
So there we are.
Let's run it again just to see and make sure that it actually puts it into the next line.
And it does it okay.
So a line break comes afterward okay.
That's why.
Well actually if you think about it.
So we had it twice and then we ran it again.
And only then did we do the line break.
Okay.
So if I delete everything from now or from the file from the log file and I run it again now it will
create the first hello world.
And then it will create the second hello world.
If I run it again.
And now both of them should be in a separate line.
All right.
There we are.
So this will now allow us to log whatever error or whatever we have onto our PC, which is pretty cool
because this is something that can be very useful whenever you are delivering the application to your
users.
And there's an error, and you want them to have the capability of sharing this log file with you.
Or you could even go as far as to logging data directly into a, um, cloud database, for example,
so that you can see the errors that users are generating.
But I'm not sure how it is data data protection wise, so you would have to check that out before actually
doing that if it's legally all right in your country or not, or in the country of your users.
Okay.
So that's it for this video.
In the next video, I'm going to show you how you can change the directory.
So the path at which you can store the file, it doesn't have to do anything with inheritance.
I just think it's good for completion so that you know how to do it.
Okay.
So see you in the next video.