Flow.Launcher/Flow.Launcher.Infrastructure/Stopwatch.cs

61 lines
2.3 KiB
C#
Raw Permalink Normal View History

using System;
2025-04-13 09:11:36 +00:00
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Logger;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure
{
public static class Stopwatch
{
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
2025-04-13 09:11:36 +00:00
public static long Debug(string className, string message, Action action, [CallerMemberName] string methodName = "")
{
2016-11-30 00:31:31 +00:00
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
action();
2016-11-30 00:31:31 +00:00
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
2025-04-13 09:11:36 +00:00
Log.Debug(className, $"{message} <{milliseconds}ms>", methodName);
2016-11-30 00:31:31 +00:00
return milliseconds;
}
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
2025-04-13 09:11:36 +00:00
public static async Task<long> DebugAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "")
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
await action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
2025-04-13 09:11:36 +00:00
Log.Debug(className, $"{message} <{milliseconds}ms>", methodName);
return milliseconds;
}
2025-04-13 09:11:36 +00:00
public static long Info(string className, string message, Action action, [CallerMemberName] string methodName = "")
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
2025-04-13 09:11:36 +00:00
Log.Info(className, $"{message} <{milliseconds}ms>", methodName);
return milliseconds;
}
2025-04-13 09:11:36 +00:00
public static async Task<long> InfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "")
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
await action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
2025-04-13 09:11:36 +00:00
Log.Info(className, $"{message} <{milliseconds}ms>", methodName);
return milliseconds;
}
}
}