7.3.5. C# MSBuild Configuration

NOTE

Sonargraph does not change or build your code! Before you analyze any C# project in Sonargraph, make sure that you can successfully build the project using Visual Studio.

Sonargraph examines the registry on a Windows machine at startup to locate the latest Visual Studio version and to extract the information to run MSBuild on the latest "Visual Studio Developer Command Prompt". See Section 4.9.1, “C# Build Executor Configuration” for more details. Sonargraph tries its best to extract all required information about project references, referenced assemblies, defined constants, etc. from the .csproj files. As the project setup can get elaborate with variables and conditions, the invocation of MSBuild with an on-demand created small wrapper project file leads to the most precise results.

Unfortunately, the invocation of MSBuild for individual projects from the command-line does not necessarily lead to the same results as building the project in Visual Studio. If MSBuild reports an error, the project file is processed with the limited built-in XML parser and an issue is created for the module. The state of the MSBuild execution per module can be examined by opening the configuration dialog via "System""Configure MSBuild...". The dialog indicates problematic modules with an error marker and reports the last time of the MSBuild execution as well as the command-line that was used by Sonargraph and the error output of MSBuild as shown in the following screenshot:

Configuration of MSBuild
Figure 7.9. Configuration of MSBuild


In case of an error, MSBuild usually reports the exact location in the .csproj file as highlighted in the above screenshot. The Visual Studio project can be easily opened via the provided link at the top of the dialog. Often, a variable is undefined that is present when the project is built in Visual Studio. Variables can be defined in the format '/p:name=value'. Those variables can be defined on different levels and can override each other (module-level overrides system-level overrides installation-level):

  1. Module: The variables are only present for the currently selected module.

  2. System: The variables are present for all C# modules that are based on Visual Studio project files.

  3. Installation: The variables are present for all Sonargraph systems that contain C# modules that are based on Visual Studio project files.

The changed configuration of MSBuild can be tested per project on the dialog. The configurations are persisted to sonargraph-msbuild.properties files that can be put under version control if the configuration is applicable for all team members.

TIP

Information about MSBuild properties and command-line options is available on the internet:

TIP

To investigate the property values that are used by Visual Studio, the output-level of MSBuild needs to be set to "diagnostic". In Visual Studio 2017, this is done via the menu "Tools""Options", and then filter for "Build". The MSBuild output verbosity can then be set on the right, as shown in the next screenshot:

Setting the MSBuild Verbosity Level in Visual Studio
Figure 7.10. Setting the MSBuild Verbosity Level in Visual Studio


If the project is now rebuilt, MSBuild logs a ton of information to the console that can be searched for the relevant property.

In order to compare the detailed output from Visual Studio with the environment info that is seen by the MSBuild executed by Sonargraph, add the following two lines as module-level properties. The information is logged in this case to the file "D:\temp\MyLog.log".

/fileLogger
/fileLoggerParameters:LogFile=D:\temp\MyLog.log;Verbosity=diagnostic;Encoding=UTF-8

More info about possible log generation is available on the official MSBuild documentation: MSBuild command-line reference