← All source files

V7 / 2a442462cf3f

BootTimingExtensions.cs

Lilith/Core/Extensions/BootTimingExtensions.cs

Unchanged from the preceding version
CS
using System.Diagnostics;

namespace Lilith.Core.Extensions;

public static class BootTimingExtensions
{
    public static async Task<T> TimeStageAsync<T>(this Logger logger, string stage, Func<Task<T>> action)
    {
        var timer = Stopwatch.StartNew();
        bool succeeded = false;
        try
        {
            T result = await action();
            succeeded = true;
            return result;
        }
        finally
        {
            timer.Stop();
            logger.WriteLine("Boot", $"{stage} {(succeeded ? "finished" : "failed")} — elapsed {timer.Elapsed.TotalSeconds:F2}s",
                succeeded ? LogColors.Gray : LogColors.Red);
        }
    }

    public static Task TimeStageAsync(this Logger logger, string stage, Func<Task> action) =>
        logger.TimeStageAsync(stage, async () => { await action(); return true; });
}