/// <summary>
        /// Closes the underlying file stream.
        /// </summary>
        public void Shutdown()
        {
            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", _initialTargetOutputLogging);
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", _initialLogImports);

            if (projectImportsCollector != null)
            {
                if (CollectProjectImports == ProjectImportsCollectionMode.Embed)
                {
                    eventArgsWriter.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, projectImportsCollector.GetAllBytes());
                }

                projectImportsCollector.Close();
                projectImportsCollector = null;
            }

            if (stream != null)
            {
                // It's hard to determine whether we're at the end of decoding GZipStream
                // so add an explicit 0 at the end to signify end of file
                stream.WriteByte((byte)BinaryLogRecordKind.EndOfFile);
                stream.Flush();
                stream.Dispose();
                stream = null;
            }
        }
        /// <summary>
        /// Closes the underlying file stream.
        /// </summary>
        public void Shutdown()
        {
            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", _initialTargetOutputLogging);
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", _initialLogImports);

            if (projectImportsCollector != null)
            {
                projectImportsCollector.Close();

                if (CollectProjectImports == ProjectImportsCollectionMode.Embed)
                {
                    var archiveFilePath = projectImportsCollector.ArchiveFilePath;

                    // It is possible that the archive couldn't be created for some reason.
                    // Only embed it if it actually exists.
                    if (File.Exists(archiveFilePath))
                    {
                        eventArgsWriter.WriteBlob(BinaryLogRecordKind.ProjectImportArchive, File.ReadAllBytes(archiveFilePath));
                        File.Delete(archiveFilePath);
                    }
                }

                projectImportsCollector = null;
            }

            if (stream != null)
            {
                // It's hard to determine whether we're at the end of decoding GZipStream
                // so add an explicit 0 at the end to signify end of file
                stream.WriteByte((byte)BinaryLogRecordKind.EndOfFile);
                stream.Flush();
                stream.Dispose();
                stream = null;
            }
        }
Example #3
0
        /// <summary>
        /// Initializes the logger and subscribes to the relevant events.
        /// </summary>
        /// <param name="eventSource">The available events that processEvent logger can subscribe to.</param>
        public override void Initialize(IEventSource eventSource)
        {
            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true");
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1");

            // Set this environment variable to log AssemblyFoldersEx search results from ResolveAssemblyReference
            // Environment.SetEnvironmentVariable("MSBUILDLOGVERBOSERARSEARCHRESULTS", "true");

            ProcessParameters();

            if (SaveLogToDisk)
            {
                try
                {
                    projectImportsCollector = new ProjectImportsCollector(_logFile);
                }
                catch (Exception ex)
                {
                    throw new LoggerException($"Failed to create the source archive for log file {_logFile}", ex);
                }
            }

            construction = new Construction();

            eventSource.BuildStarted      += construction.BuildStarted;
            eventSource.BuildFinished     += construction.BuildFinished;
            eventSource.ProjectStarted    += construction.ProjectStarted;
            eventSource.ProjectFinished   += construction.ProjectFinished;
            eventSource.TargetStarted     += construction.TargetStarted;
            eventSource.TargetFinished    += construction.TargetFinished;
            eventSource.TaskStarted       += construction.TaskStarted;
            eventSource.TaskFinished      += construction.TaskFinished;
            eventSource.MessageRaised     += construction.MessageRaised;
            eventSource.WarningRaised     += construction.WarningRaised;
            eventSource.ErrorRaised       += construction.ErrorRaised;
            eventSource.CustomEventRaised += construction.CustomEventRaised;
            eventSource.StatusEventRaised += construction.StatusEventRaised;

            if (projectImportsCollector != null)
            {
                eventSource.AnyEventRaised += EventSource_AnyEventRaised;
            }

            var assembly = typeof(BuildEventArgs).GetTypeInfo().Assembly;

            projectImportedEventArgsType = assembly.GetType("Microsoft.Build.Framework.ProjectImportedEventArgs");
            if (projectImportedEventArgsType != null)
            {
                importedProjectFile = projectImportedEventArgsType.GetProperty("ImportedProjectFile", BindingFlags.Public | BindingFlags.Instance);
                unexpandedProject   = projectImportedEventArgsType.GetProperty("UnexpandedProject", BindingFlags.Public | BindingFlags.Instance);
            }

            projectEvaluationFinishedEventArgsType = assembly.GetType("Microsoft.Build.Framework.ProjectEvaluationFinishedEventArgs");
            if (projectEvaluationFinishedEventArgsType != null)
            {
                projectEvaluationFinishedProjectFile    = projectEvaluationFinishedEventArgsType.GetProperty("ProjectFile", BindingFlags.Public | BindingFlags.Instance);
                projectEvaluationFinishedProfilerResult = projectEvaluationFinishedEventArgsType.GetProperty("ProfilerResult", BindingFlags.Public | BindingFlags.Instance);
            }
        }
        /// <summary>
        /// Initializes the logger by subscribing to events of IEventSource
        /// </summary>
        public void Initialize(IEventSource eventSource)
        {
            _initialTargetOutputLogging = Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING");
            _initialLogImports          = Environment.GetEnvironmentVariable("MSBUILDLOGIMPORTS");

            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true");
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1");

            ProcessParameters();

            try
            {
                string logDirectory = null;
                try
                {
                    logDirectory = Path.GetDirectoryName(FilePath);
                }
                catch (Exception)
                {
                    // Directory creation is best-effort; if finding its path fails don't create the directory
                    // and possibly let the FileStream constructor below report the failure
                }

                if (logDirectory != null)
                {
                    Directory.CreateDirectory(logDirectory);
                }

                stream = new FileStream(FilePath, FileMode.Create);

                if (CollectProjectImports != ProjectImportsCollectionMode.None)
                {
                    projectImportsCollector = new ProjectImportsCollector(FilePath);
                }

                if (eventSource is IEventSource3 eventSource3)
                {
                    eventSource3.IncludeEvaluationMetaprojects();
                }
            }
            catch (Exception e)
            {
                string errorCode;
                string helpKeyword;
                string message = ResourceUtilities.FormatResourceString(out errorCode, out helpKeyword, "InvalidFileLoggerFile", FilePath, e.Message);
                throw new LoggerException(message, e, errorCode, helpKeyword);
            }

            stream          = new GZipStream(stream, CompressionLevel.Optimal);
            binaryWriter    = new BinaryWriter(stream);
            eventArgsWriter = new BuildEventArgsWriter(binaryWriter);

            binaryWriter.Write(FileFormatVersion);

            LogInitialInfo();

            eventSource.AnyEventRaised += EventSource_AnyEventRaised;
        }
Example #5
0
        /// <summary>
        /// Initializes the logger and subscribes to the relevant events.
        /// </summary>
        /// <param name="eventSource">The available events that processEvent logger can subscribe to.</param>
        public override void Initialize(IEventSource eventSource)
        {
            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true");
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1");

            // Set this environment variable to log AssemblyFoldersEx search results from ResolveAssemblyReference
            // Environment.SetEnvironmentVariable("MSBUILDLOGVERBOSERARSEARCHRESULTS", "true");

            ProcessParameters();

            if (SaveLogToDisk)
            {
                try
                {
                    projectImportsCollector = new ProjectImportsCollector(_logFile);
                }
                catch (Exception ex)
                {
                    throw new LoggerException($"Failed to create the source archive for log file {_logFile}", ex);
                }
            }

            construction = new Construction();

            Strings.Initialize();

            eventSource.BuildStarted      += construction.BuildStarted;
            eventSource.BuildFinished     += construction.BuildFinished;
            eventSource.ProjectStarted    += construction.ProjectStarted;
            eventSource.ProjectFinished   += construction.ProjectFinished;
            eventSource.TargetStarted     += construction.TargetStarted;
            eventSource.TargetFinished    += construction.TargetFinished;
            eventSource.TaskStarted       += construction.TaskStarted;
            eventSource.TaskFinished      += construction.TaskFinished;
            eventSource.MessageRaised     += construction.MessageRaised;
            eventSource.WarningRaised     += construction.WarningRaised;
            eventSource.ErrorRaised       += construction.ErrorRaised;
            eventSource.CustomEventRaised += construction.CustomEventRaised;
            eventSource.StatusEventRaised += construction.StatusEventRaised;

            if (projectImportsCollector != null)
            {
                eventSource.AnyEventRaised += EventSource_AnyEventRaised;
            }
        }
Example #6
0
        public override void Shutdown()
        {
            base.Shutdown();

            if (projectImportsCollector != null)
            {
                var archiveFilePath = projectImportsCollector.ArchiveFilePath;

                projectImportsCollector.Close();
                projectImportsCollector = null;

                if (File.Exists(archiveFilePath))
                {
                    var bytes = File.ReadAllBytes(archiveFilePath);
                    construction.Build.SourceFilesArchive = bytes;
                    File.Delete(archiveFilePath);
                }
            }

            if (SaveLogToDisk)
            {
                try
                {
                    if (Path.IsPathRooted(_logFile))
                    {
                        var parentDirectory = Path.GetDirectoryName(_logFile);
                        if (!Directory.Exists(parentDirectory))
                        {
                            Directory.CreateDirectory(parentDirectory);
                        }
                    }

                    Serialization.Write(construction.Build, _logFile);
                }
                catch (Exception ex)
                {
                    ErrorReporting.ReportException(ex);
                }
            }
            else
            {
                CurrentBuild = construction.Build;
            }
        }
        /// <summary>
        /// Initializes the logger by subscribing to events of IEventSource
        /// </summary>
        public void Initialize(IEventSource eventSource)
        {
            _initialTargetOutputLogging = Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING");
            _initialLogImports          = Environment.GetEnvironmentVariable("MSBUILDLOGIMPORTS");

            Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true");
            Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1");

            ProcessParameters();

            try
            {
                string logDirectory = null;
                try
                {
                    logDirectory = Path.GetDirectoryName(FilePath);
                }
                catch (Exception)
                {
                    // Directory creation is best-effort; if finding its path fails don't create the directory
                    // and possibly let the FileStream constructor below report the failure
                }

                if (logDirectory != null)
                {
                    Directory.CreateDirectory(logDirectory);
                }

                stream = new FileStream(FilePath, FileMode.Create);

                if (CollectProjectImports != ProjectImportsCollectionMode.None)
                {
                    projectImportsCollector = new ProjectImportsCollector(FilePath, CollectProjectImports == ProjectImportsCollectionMode.ZipFile);
                }

                if (eventSource is IEventSource3 eventSource3)
                {
                    eventSource3.IncludeEvaluationMetaprojects();
                }
            }
            catch (Exception e)
            {
                string errorCode;
                string helpKeyword;
                string message = ResourceUtilities.FormatResourceString(out errorCode, out helpKeyword, "InvalidFileLoggerFile", FilePath, e.Message);
                throw new LoggerException(message, e, errorCode, helpKeyword);
            }

            stream = new GZipStream(stream, CompressionLevel.Optimal);

            // wrapping the GZipStream in a buffered stream significantly improves performance
            // and the max throughput is reached with a 32K buffer. See details here:
            // https://github.com/dotnet/runtime/issues/39233#issuecomment-745598847
            stream          = new BufferedStream(stream, bufferSize: 32768);
            binaryWriter    = new BinaryWriter(stream);
            eventArgsWriter = new BuildEventArgsWriter(binaryWriter);

            binaryWriter.Write(FileFormatVersion);

            LogInitialInfo();

            eventSource.AnyEventRaised += EventSource_AnyEventRaised;
        }