Find out how to add your own information into the build output, as well as get more out of the standard results by jumping directly to source of the problem.
The build output window is very important to developers. It tells us when we have errors and warnings in our builds, provides a means of tracking those issues down, and gives us a view into the progress of the build process.
You have the capability of modifying the output of the build results to provide some extra information. All you need to do is tap into the build events [Hack #34] and add your own output. For example, a failed build of a project looks like the screen shown in Figure 4-27.
If you use the hack to stop a build process when one of the project builds fail [Hack #34] , then on a large solution build, you'll want to know exactly which project is causing the build to die. Starting with the code from [Hack #34], the code in the Macro Explorer looks like this:
Private Sub BuildEvents_OnBuildProjConfigDone( _ ByVal Project As String, _ ByVal ProjectConfig As String, _ ByVal Platform As String, _ ByVal SolutionConfig As String, _ ByVal Success As Boolean) _ Handles BuildEvents.OnBuildProjConfigDone If Success = False Then 'The build failed...cancel any further builds. DTE.ExecuteCommand("Build.Cancel") End If End Sub
You want to add some code to add a little more information into the output window. To do this, you simply need to add the following lines of code:
Dim win As Window = _ DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) Dim OW As OutputWindow = CType(win.Object, OutputWindow) OW.OutputWindowPanes.Item("Build").OutputString( _ String.Format( _ "Build Stopped with a failure on the {0} project. {1}", _ Project, _ System.Environment.NewLine))
This code gets a reference to the output windows, then the Build output windows specifically. It then uses the OutputString method to write out a formatted string indicating that the build failed and on what project.
|
The final code looks like this:
Private Sub BuildEvents_OnBuildProjConfigDone( _ ByVal Project As String, _ ByVal ProjectConfig As String, _ ByVal Platform As String, _ ByVal SolutionConfig As String, _ ByVal Success As Boolean) _ Handles BuildEvents.OnBuildProjConfigDone If Success = False Then Dim win As Window = _ DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) Dim OW As OutputWindow = CType(win.Object, OutputWindow) OW.OutputWindowPanes.Item("Build").OutputString( _ String.Format( _ "Build Stopped with a failure on the {0} project. {1}", _ Project, System.Environment.NewLine)) 'The build failed...cancel any further builds. DTE.ExecuteCommand("Build.Cancel") DTE.ExecuteCommand("View.Output") End If End Sub
The output on a failed build now looks like the screen shown in Figure 4-28.
Of course, on a single project build this hack doesn't help much, but if you have a large number of build errors this little hack will quickly show you at the bottom of your results which project is the culprit.
You can use this technique to write information to any of the IDE windows you wish. Just modify which window you assign to the variable win by altering the constant value for the window, or change the item you are looking for in the OutputWindowPanes collection (to write to the Database output window, for example). By altering the output to the Build output window, you can provide yourself with more information about the build, including the progress of any processes you have added.
By default, Visual Studio will list all of the build warnings and errors as tasks within the Task List window after a build. This task list can then be used to navigate to the specific lines of code that are causing those warning and errors; however, there is also another way to navigate the build resultsby using the build results in the Build output window.
To navigate the errors listed in the Build output window if the build results have scrolled in the window, you can use the F8 key (Edit.GoToNextLocation) to move the next error or warning and Shift-F8 (Edit.GoToPreviousLocation) to move to the previous one. This will also bring up the source file and position the cursor on the error.
You can also double-click on an error or warning listed in the Build output window and the IDE will bring up that source code file and place the cursor on the line indicated by the build results. The same effect can be accomplished by right-clicking the Build output window line and choosing Go To Error or Go To Tag for the same result.
Michael Wood