我想知道如何在运行时构建C#项目后获取BuildResult
的错误。
在妈妈那里我有这个:
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
文件,但是文件总是空的(有错误与否)。
由于
答案 0 :(得分:5)
这个有点晚了,但对于其他试图这样做的人,我遇到了同样的问题。出于某种原因,当您使用ProjectCollection注册记录器时,它不会通过构造函数传递给BuildParamters对象 - 在检查时,新的BuildParameters(pc).loggers为null。这感觉就像一个hack,但我只是在BuildParameter对象上手动设置logger集合,如下所示:
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,否则您将收到有关尝试访问日志文件的多个进程的错误。