/// <summary> /// Initializes a new instance of the CBTBuildEngine class. /// </summary> public CBTBuildEngine() { ContinueOnError = false; LineNumberOfTaskNode = 0; ProjectFileOfTaskNode = String.Empty; ColumnNumberOfTaskNode = 0; // Get the current build manager and use reflection to get the LoggingService and LogBuildEvent() method // BuildManager buildManager = BuildManager.DefaultBuildManager; PropertyInfo loggingServiceProperty = buildManager.GetType().GetProperty("Microsoft.Build.BackEnd.IBuildComponentHost.LoggingService", BindingFlags.Instance | BindingFlags.NonPublic); if (loggingServiceProperty != null) { try { object loggingService = loggingServiceProperty.GetMethod.Invoke(buildManager, null); MethodInfo logBuildEventMethod = loggingService?.GetType().GetMethod("LogBuildEvent", new[] {typeof (BuildEventArgs)}); if (logBuildEventMethod != null) { _logBuildEventHandler = (LogBuildEventHandler) logBuildEventMethod.CreateDelegate(typeof (LogBuildEventHandler), loggingService); } } catch (Exception e) { Trace.TraceError(e.ToString()); } } }
/// <summary> /// Initializes a new instance of the CBTBuildEngine class. /// </summary> public CBTBuildEngine() { ContinueOnError = false; LineNumberOfTaskNode = 0; ProjectFileOfTaskNode = String.Empty; ColumnNumberOfTaskNode = 0; // Get the current build manager and use reflection to get the LoggingService and LogBuildEvent() method // BuildManager buildManager = BuildManager.DefaultBuildManager; PropertyInfo loggingServiceProperty = buildManager?.GetType().GetProperty("Microsoft.Build.BackEnd.IBuildComponentHost.LoggingService", BindingFlags.Instance | BindingFlags.NonPublic); if (loggingServiceProperty != null) { try { object loggingService = null; try { loggingService = loggingServiceProperty.GetMethod.Invoke(buildManager, null); } catch (TargetInvocationException e) when(e.InnerException is NullReferenceException) { // When a build is not taking place, there is no logging service. The LoggingService property attempts to cast which throws a NullReferenceException so this is ignored. } MethodInfo logBuildEventMethod = loggingService?.GetType().GetMethod("LogBuildEvent", new[] { typeof(BuildEventArgs) }); if (logBuildEventMethod != null) { _logBuildEventHandler = (LogBuildEventHandler)logBuildEventMethod.CreateDelegate(typeof(LogBuildEventHandler), loggingService); } } catch (Exception e) { Trace.TraceError(e.ToString()); } } }