/// <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 (FileSystems.Default.FileExists(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; } }
/// <summary> /// Closes the underlying file stream. /// </summary> public void Shutdown() { Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", _initialTargetOutputLogging); Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", _initialLogImports ? "1" : ""); Traits.Instance.EscapeHatches.LogProjectImports = _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() { 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; } }
/// <summary> /// Initializes the logger by subscribing to events of IEventSource /// </summary> public void Initialize(IEventSource eventSource) { Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true"); Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1"); ProcessParameters(); try { stream = new FileStream(FilePath, FileMode.Create); if (CollectProjectImports != ProjectImportsCollectionMode.None) { projectImportsCollector = new ProjectImportsCollector(FilePath); } } catch (Exception e) { string errorCode = ""; string helpKeyword = ""; string message = 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); eventSource.AnyEventRaised += EventSource_AnyEventRaised; }
/// <summary> /// Initializes the logger by subscribing to events of IEventSource /// </summary> public void Initialize(IEventSource eventSource) { _initialTargetOutputLogging = Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING"); _initialLogImports = Traits.Instance.EscapeHatches.LogProjectImports; Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true"); Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1"); Traits.Instance.EscapeHatches.LogProjectImports = true; 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.FormatResourceStringStripCodeAndKeyword(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; }
public static void IncludeSourceFiles(this ProjectImportsCollector projectImportsCollector, BuildEventArgs e) { if (e is TaskStartedEventArgs) { var taskArgs = (TaskStartedEventArgs)e; projectImportsCollector.AddFile(taskArgs.TaskFile); } else if (e is TargetStartedEventArgs) { var targetArgs = (TargetStartedEventArgs)e; projectImportsCollector.AddFile(targetArgs.TargetFile); } else if (e is ProjectStartedEventArgs) { var projectStarted = (ProjectStartedEventArgs)e; projectImportsCollector.AddFile(projectStarted.ProjectFile); } }
/// <summary> /// Initializes the logger by subscribing to events of IEventSource /// </summary> public void Initialize(IEventSource eventSource) { _initialTargetOutputLogging = Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING"); _initialLogImports = Traits.Instance.EscapeHatches.LogProjectImports; Environment.SetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING", "true"); Environment.SetEnvironmentVariable("MSBUILDLOGIMPORTS", "1"); Traits.Instance.EscapeHatches.LogProjectImports = true; bool logPropertiesAndItemsAfterEvaluation = Traits.Instance.EscapeHatches.LogPropertiesAndItemsAfterEvaluation ?? true; 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(); } if (logPropertiesAndItemsAfterEvaluation && eventSource is IEventSource4 eventSource4) { eventSource4.IncludeEvaluationPropertiesAndItems(); } } catch (Exception e) { string errorCode; string helpKeyword; string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(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; }