5

I have a bunch of solutions downloaded from the Internet (codeplex etc.) and I want to build them and run a tool over the DLLs. I want to do this via automation.

It has been suggested to use MSBuild API because it will make it easy to get error information and manipulate MsBuild futher to obtain other info. Unfortunately it is hardly documented so:

  1. How do I build an .sln file (via MSBuild API 4.0) ?
  2. How do I capture the error information? (I saw an example on how to output the log to the console, but did not find smth for files)

Thanks!

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74

3 Answers3

4

I found a related question on stackoverflow that provides the solution:

running msbuild programmatically

The accepted answer provides good resources:

http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/ec95c513-f972-45ad-b108-5fcfd27f39bc/ Logging Build messages with MSBuild 4.0: http://www.go4answers.com/Example/building-solution-programatically-vs-5395.aspx

Also, there is an example of log usage on msdn: http://msdn.microsoft.com/en-us/library/microsoft.build.framework.ilogger.aspx

Community
  • 1
  • 1
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74
3

Maybe I'm missing something, but why does it have to be the MSBuild API for a simple task like that?

Just from what you've written in the question, I see no need to use the API just to build a solution and capture the output in a text file.
You can use the MSBuild command line tool for this.

Building a solution with MSBuild is as simple as that:

%windir%\Microsoft.net\Framework\v4.0.30319\msbuild.exe MySolution.sln

To capture the output in a text file, you just need to add this:
(example copied from the link)

/l:FileLogger,Microsoft.Build;logfile=MyLog.log

So the final statement looks like this:

%windir%\Microsoft.net\Framework\v4.0.30319\msbuild.exe MySolution.sln /l:FileLogger,Microsoft.Build;logfile=MyLog.log

This will build the solution and save the output of MSBuild in a text file named MyLog.log in the current directory.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • Fair enough, I know how to do it by running a command line, but for automation purposes using MSBuild API will provide more control - I plan to inject some tasks later on for example. – Bogdan Gavril MSFT Mar 14 '12 at 08:51
  • @Christian Specht - Because shelling out to the console for anything is an extra complexity that can be avoided. – StingyJack Jul 03 '17 at 16:37
1

Nothing that you've described requires the use of the MSBuild API. Using the MS Build API is worthwhile if you want to extend the build process for custom build tasks, but it is not useful if you simply want to automate your builds. The API is very well documented. Here's a like to the MSDN documentation. MSBuild API

To answer your questions directly: 1) MSBuild does not understand solution files. Those are a visual studio concept. MSBuild will 'run' a solution file if built from the command line, but it does that by generating a msbuild file from the solution file. You'd get the same effect by running your toplevel project files directly.

2) As mentioned my Christian, you can do that with a logger. Here's the documentation: Build Logger

If you want to automate your builds, use an established build automation environment like Jenkins or TeamCity.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54