using System.Diagnostics; namespace MsbuildAnalyzer.Utilities; /// /// Performance logging utility that writes timing information to stderr when NX_PERF_LOGGING=true. /// Follows Nx performance logging conventions. /// public class PerfLogger : IDisposable { private readonly string _operationName; private readonly Stopwatch _stopwatch; private static readonly bool _isEnabled = Environment.GetEnvironmentVariable("NX_PERF_LOGGING") == "true"; private PerfLogger(string operationName) { _operationName = operationName; _stopwatch = Stopwatch.StartNew(); } /// /// Start timing an operation. Use with 'using' statement for automatic disposal. /// /// Name of the operation being timed. /// PerfLogger instance that will log timing on disposal. public static PerfLogger? Start(string operationName) { return _isEnabled ? new PerfLogger(operationName) : null; } /// /// Logs the elapsed time for the operation to stderr. /// public void Dispose() { if (_isEnabled) { _stopwatch.Stop(); Console.Error.WriteLine($"Time for '[@nx/dotnet]: {_operationName}' {_stopwatch.Elapsed.TotalMilliseconds:F1}ms"); } } }