1 00:00:00,810 --> 00:00:02,090 One technique we can use, 2 00:00:02,090 --> 00:00:05,670 the debugger application, is by logging to the Output window. 3 00:00:05,670 --> 00:00:09,549 This will allow us to get an idea of what's going on our application without 4 00:00:09,549 --> 00:00:12,641 actually having to stop or pause the execution of the code. 5 00:00:12,641 --> 00:00:16,250 There are a couple ways we can write to the Output window. 6 00:00:16,250 --> 00:00:19,790 The first is by calling the method everywhere we want to log. 7 00:00:19,790 --> 00:00:23,250 A log is simply a list of messages that we can look through at a later 8 00:00:23,250 --> 00:00:27,140 point to try to figure out what is happening when a program is running. 9 00:00:27,140 --> 00:00:28,320 Let's write some logs right now. 10 00:00:30,120 --> 00:00:30,950 To write logs for 11 00:00:30,950 --> 00:00:35,860 debugging purposes, we can use the Writeline method of the Debug Class. 12 00:00:35,860 --> 00:00:39,220 It's found in the System.Diagnostic namespace. 13 00:00:39,220 --> 00:00:46,255 So right here, let's put using System.Diagnostics. 14 00:00:46,255 --> 00:00:50,790 Let's log the user's menu selection and song name and program .CS. 15 00:00:50,790 --> 00:00:54,111 Our logs will be visible in what's known as the Output window. 16 00:00:54,111 --> 00:00:57,460 We'll go into more detail about the output window later in the workshop. 17 00:00:57,460 --> 00:01:00,730 For now, just know that this is a place where we can find information 18 00:01:00,730 --> 00:01:03,680 about how our app is running while it's debugging. 19 00:01:03,680 --> 00:01:06,900 Let's print out the options selected by the user. 20 00:01:06,900 --> 00:01:11,310 So right down here after we get the option, 21 00:01:11,310 --> 00:01:14,864 we can write Debug.Writeline and 22 00:01:14,864 --> 00:01:19,396 I'll use the format strings syntax here and 23 00:01:19,396 --> 00:01:26,060 say Option selected, And print out the option. 24 00:01:27,160 --> 00:01:31,750 And down here, we can print out the song name that the user entered. 25 00:01:32,770 --> 00:01:38,908 So we'll write Debug.Writeline and 26 00:01:38,908 --> 00:01:43,710 say New song added songName. 27 00:01:44,930 --> 00:01:48,450 Now let's run the app with the debugger by clicking on the play button here. 28 00:01:50,380 --> 00:01:55,080 So the app starts up and Visual Studio switches into debug mode. 29 00:01:55,080 --> 00:01:57,690 You can see that there are a few more windows open. 30 00:01:57,690 --> 00:02:00,468 You've got the Diagnostic Tools window, that Call Stack, Autos. 31 00:02:01,680 --> 00:02:04,670 We can close this Diagnostic Tools window. 32 00:02:04,670 --> 00:02:09,351 And down here, we see a tab for the Output window. 33 00:02:09,351 --> 00:02:13,780 Let's expanded it a little bit, so we can see what's going on here. 34 00:02:13,780 --> 00:02:19,810 Now I'll switch back to the consul and let's add a song. 35 00:02:19,810 --> 00:02:24,470 So here when I selected option 1, you can see our log was printed to the output. 36 00:02:25,490 --> 00:02:29,755 Now say Learning To Fly, And 37 00:02:29,755 --> 00:02:34,580 when I hit Enter, we see another log, a new song added Learning To Fly. 38 00:02:37,960 --> 00:02:42,811 Now if I quit and stop the application, the Debugger also stops running and 39 00:02:42,811 --> 00:02:48,410 those other windows close, but the Output window is still available here. 40 00:02:48,410 --> 00:02:51,670 We can see the logs that we wrote while the program was running. 41 00:02:52,880 --> 00:02:56,860 And here's the option selected 1 and New song added, Learning to Fly. 42 00:02:58,830 --> 00:03:02,370 Using this methodology works well in the simplest of scenarios, but 43 00:03:02,370 --> 00:03:05,910 it can lead to issues for your project if you're not careful. 44 00:03:05,910 --> 00:03:09,540 One problem is that for each spot in the code where we want to log 45 00:03:09,540 --> 00:03:13,640 the Output window, we've written a new line of code to do so. 46 00:03:13,640 --> 00:03:17,930 Over time if this code is forgotten and left in the codebase, the application may 47 00:03:17,930 --> 00:03:21,700 end up littered with calls that you don't necessarily want or need any longer. 48 00:03:22,780 --> 00:03:26,490 Similar to the first problem is managing the log statements. 49 00:03:26,490 --> 00:03:29,618 We don't have a central place to manage all of the log statements. 50 00:03:29,618 --> 00:03:33,367 When you use this technique, just remember to clean up afterwards, so 51 00:03:33,367 --> 00:03:36,503 you don't leave any unnecessary statements in your code. 52 00:03:36,503 --> 00:03:40,728 There's another way to lock the output window called tracepoint. 53 00:03:40,728 --> 00:03:44,273 Tracepoints are break points that have custom actions. 54 00:03:44,273 --> 00:03:46,663 If you don't know what a breakpoint is don't worry, 55 00:03:46,663 --> 00:03:50,380 we'll be covering breakpoints in great detail later in the workshop. 56 00:03:50,380 --> 00:03:54,550 For now, just know that a breakpoint is a marker set on a line of code 57 00:03:54,550 --> 00:03:56,370 that Visual Studio can take some actions on. 58 00:03:57,610 --> 00:04:02,640 Let's remove the calls to Debug.WriteLine and replace them with tracepoints. 59 00:04:02,640 --> 00:04:06,530 We won't be needing this using System.Diagnostics line either. 60 00:04:08,450 --> 00:04:12,850 To create a tracepoint, we first need to create a breakpoint. 61 00:04:12,850 --> 00:04:17,519 To create a breakpoint, we'll find the line that is right after where we would 62 00:04:17,519 --> 00:04:22,490 have put the Debug.WriteLine statement and click on this gray bar on the far left. 63 00:04:22,490 --> 00:04:26,140 I'm gonna copy this message right here because we'll be using that later. 64 00:04:28,200 --> 00:04:31,010 And we can delete the Debug.WriteLine. 65 00:04:31,010 --> 00:04:33,230 To turn the breakpoint into a tracepoint, 66 00:04:33,230 --> 00:04:36,930 we need to tell the debugger to do something when this breakpoint is hit. 67 00:04:36,930 --> 00:04:42,200 To add an action to a breakpoint, just right click on it, select Actions. 68 00:04:42,200 --> 00:04:46,170 Now here we see the Settings Panel for breakpoints, 69 00:04:46,170 --> 00:04:49,170 make sure that the Actions checkbox is checked. 70 00:04:49,170 --> 00:04:51,800 And in the text field, we'll paste our message. 71 00:04:53,130 --> 00:04:58,560 To make this a tracepoint, we want to have this Continue execution checkbox checked, 72 00:04:58,560 --> 00:05:01,655 otherwise the execution will be paused when it gets to the breakpoint. 73 00:05:03,570 --> 00:05:05,520 Let's create another tracepoint down here. 74 00:05:06,980 --> 00:05:11,490 So here where we say New song added, we'll copy that, 75 00:05:12,820 --> 00:05:19,520 we'll delete this line, And we'll put a tracepoint right here. 76 00:05:19,520 --> 00:05:23,430 Another way to add a tracepoint is just to right-click on the line where you want 77 00:05:23,430 --> 00:05:28,580 the tracepoint, go down to Breakpoint and click Insert Tracepoint. 78 00:05:28,580 --> 00:05:29,960 Now I can paste our message right here. 79 00:05:32,560 --> 00:05:36,060 You can quickly identify tracepoints in Visual Studio because they have 80 00:05:36,060 --> 00:05:40,780 a diamond-shaped marker instead of the circle marker of standard breakpoints. 81 00:05:40,780 --> 00:05:44,740 Now when we run the application, we'll see our output is the same. 82 00:05:44,740 --> 00:05:48,804 And a huge benefit is that we no longer need to pollute our code with extra 83 00:05:48,804 --> 00:05:51,391 statements to log some debugging messages.