3

我想知道如何BuildResult在运行时构建 C# 项目后得到错误。

目前我有这个:

string projectFileName = @"C:\Users\Inspire\Documents\Mono Projects\Library\Library\Library.csproj";
FileLogger fileLogger = new FileLogger();
fileLogger.Parameters = @"logfile=" + @"C:\Users\Inspire\Documents\Mono Projects\Library\Library\log.text";

ProjectCollection projectCollection = new ProjectCollection();
projectCollection.RegisterLogger(fileLogger);

Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();       
GlobalProperty.Add("Configuration", "Debug");       
GlobalProperty.Add("Platform", "x86");

BuildRequestData buildRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(projectCollection), buildRequest);
projectCollection.UnregisterAllLoggers();
Console.WriteLine(buildResult.OverallResult.ToString());

                    /* TODO: handle errors */

我想知道错误,以便在构建后处理它们(这里是注释部分“处理错误”),FileLogger如您所见,我添加了一个文件,但该文件始终为空(有或没有错误)。

谢谢

4

1 回答 1

6

这个有点晚了,但是对于其他尝试这样做的人,我遇到了同样的问题。出于某种原因,当您向 ProjectCollection 注册记录器时,它不会通过构造函数传递到 BuildParamters 对象中 - 经过检查,new BuildParameters(pc).loggers 为空。感觉就像是 hack,但我只是手动在 BuildParameter 对象上设置记录器集合,如下所示:

FileLogger fl = new FileLogger() { Parameters = @"logfile=somepath" };

Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "x86");

BuildRequestData BuildRequest = new BuildRequestData(slnPath, GlobalProperty, null, new string[] { "Build" }, null);

BuildParameters bp = new BuildParameters();
bp.Loggers = new List<Microsoft.Build.Framework.ILogger> { fl }.AsEnumerable();

BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuildRequest);

如果您使用此方法,请确保不要调用 pc.RegisterLogger,否则您将收到有关多个进程尝试访问日志文件的错误。

于 2013-05-24T17:37:10.470 回答