public RedirectionScope(BuildEventArgsWriter buildEventArgsWriter) { _writer = buildEventArgsWriter; }
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); if (projectImportsCollector != null) { eventArgsWriter.EmbedFile += EventArgsWriter_EmbedFile; } binaryWriter.Write(FileFormatVersion); LogInitialInfo(); eventSource.AnyEventRaised += EventSource_AnyEventRaised; }