This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
There are several techniques to debugging. In this video, we’ll go over a common technique: logging.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
One technique we can use,
0:00
the debugger application,
is by logging to the Output window.
0:02
This will allow us to get an idea of
what's going on our application without
0:05
actually having to stop or
pause the execution of the code.
0:09
There are a couple ways we can
write to the Output window.
0:12
The first is by calling the method
everywhere we want to log.
0:16
A log is simply a list of messages
that we can look through at a later
0:19
point to try to figure out what is
happening when a program is running.
0:23
Let's write some logs right now.
0:27
To write logs for
0:30
debugging purposes, we can use
the Writeline method of the Debug Class.
0:30
It's found in
the System.Diagnostic namespace.
0:35
So right here,
let's put using System.Diagnostics.
0:39
Let's log the user's menu selection and
song name and program .CS.
0:46
Our logs will be visible in what's
known as the Output window.
0:50
We'll go into more detail about
the output window later in the workshop.
0:54
For now, just know that this is
a place where we can find information
0:57
about how our app is running
while it's debugging.
1:00
Let's print out the options
selected by the user.
1:03
So right down here after
we get the option,
1:06
we can write Debug.Writeline and
1:11
I'll use the format
strings syntax here and
1:14
say Option selected, And
print out the option.
1:19
And down here, we can print out
the song name that the user entered.
1:27
So we'll write Debug.Writeline and
1:32
say New song added songName.
1:38
Now let's run the app with the debugger
by clicking on the play button here.
1:44
So the app starts up and
Visual Studio switches into debug mode.
1:50
You can see that there
are a few more windows open.
1:55
You've got the Diagnostic Tools window,
that Call Stack, Autos.
1:57
We can close this Diagnostic Tools window.
2:01
And down here, we see a tab for
the Output window.
2:04
Let's expanded it a little bit, so
we can see what's going on here.
2:09
Now I'll switch back to the consul and
let's add a song.
2:13
So here when I selected option 1, you can
see our log was printed to the output.
2:19
Now say Learning To Fly, And
2:25
when I hit Enter, we see another log,
a new song added Learning To Fly.
2:29
Now if I quit and stop the application,
the Debugger also stops running and
2:37
those other windows close, but
the Output window is still available here.
2:42
We can see the logs that we wrote
while the program was running.
2:48
And here's the option selected 1 and
New song added, Learning to Fly.
2:52
Using this methodology works well
in the simplest of scenarios, but
2:58
it can lead to issues for
your project if you're not careful.
3:02
One problem is that for
each spot in the code where we want to log
3:05
the Output window,
we've written a new line of code to do so.
3:09
Over time if this code is forgotten and
left in the codebase, the application may
3:13
end up littered with calls that you don't
necessarily want or need any longer.
3:17
Similar to the first problem is
managing the log statements.
3:22
We don't have a central place to
manage all of the log statements.
3:26
When you use this technique,
just remember to clean up afterwards, so
3:29
you don't leave any unnecessary
statements in your code.
3:33
There's another way to lock
the output window called tracepoint.
3:36
Tracepoints are break points
that have custom actions.
3:40
If you don't know what
a breakpoint is don't worry,
3:44
we'll be covering breakpoints in
great detail later in the workshop.
3:46
For now, just know that a breakpoint
is a marker set on a line of code
3:50
that Visual Studio can
take some actions on.
3:54
Let's remove the calls to Debug.WriteLine
and replace them with tracepoints.
3:57
We won't be needing this using
System.Diagnostics line either.
4:02
To create a tracepoint,
we first need to create a breakpoint.
4:08
To create a breakpoint, we'll find the
line that is right after where we would
4:12
have put the Debug.WriteLine statement and
click on this gray bar on the far left.
4:17
I'm gonna copy this message right here
because we'll be using that later.
4:22
And we can delete the Debug.WriteLine.
4:28
To turn the breakpoint into a tracepoint,
4:31
we need to tell the debugger to do
something when this breakpoint is hit.
4:33
To add an action to a breakpoint,
just right click on it, select Actions.
4:36
Now here we see the Settings Panel for
breakpoints,
4:42
make sure that the Actions
checkbox is checked.
4:46
And in the text field,
we'll paste our message.
4:49
To make this a tracepoint, we want to have
this Continue execution checkbox checked,
4:53
otherwise the execution will be paused
when it gets to the breakpoint.
4:58
Let's create another tracepoint down here.
5:03
So here where we say New song added,
we'll copy that,
5:06
we'll delete this line, And
we'll put a tracepoint right here.
5:12
Another way to add a tracepoint is just
to right-click on the line where you want
5:19
the tracepoint, go down to Breakpoint and
click Insert Tracepoint.
5:23
Now I can paste our message right here.
5:28
You can quickly identify tracepoints
in Visual Studio because they have
5:32
a diamond-shaped marker instead of
the circle marker of standard breakpoints.
5:36
Now when we run the application,
we'll see our output is the same.
5:40
And a huge benefit is that we no longer
need to pollute our code with extra
5:44
statements to log some debugging messages.
5:48
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up