-
[C#] Time CheckC#/기초 2024. 1. 24. 10:52
#Log
https://dlsenfl.tistory.com/entry/C-Logger
namespace PMCommon.Models; public class TimeSpent { private static Stopwatch stopwatch; private static bool isLogStarted = false; #if DEBUG public static string ProgramMode = "DebugMode"; #else public static string ProgramMode = "ReleaseMode"; #endif private static void TimeCheckMode() { if (!isLogStarted) { ILog log = LogManager.GetLogger("TimeCheck"); log.Info("=================" + ProgramMode + "================="); isLogStarted = true; } } #region ByStartStop public static void StartStopwatch() { stopwatch = Stopwatch.StartNew(); } public static void EndStopwatch() { stopwatch.Stop(); } public static string GetStopwatchTime() { //TimeCheckMode(); TimeSpan spentTime = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds); //return Math.Round(spentTime.TotalSeconds, 2).ToString(); return spentTime.TotalSeconds.ToString("0.00"); } /// <summary> /// Write Start/End TimeSpan to Log.txt /// </summary> public static void WriteStopwatchTimeToLog() { EndStopwatch(); MethodBase caller = new StackTrace().GetFrame(1).GetMethod(); ILog log = LogManager.GetLogger(caller.DeclaringType); log.Info(caller.Name + ": " + GetStopwatchTime() + "(s)"); } #endregion ByStartStop #region ByMethod /// <summary> /// ex) TimeCheck(()=> Func<T>, out value); /// <para> Return Func value && timespan && Write to Log.txt</para> /// </summary> /// <typeparam name="T">return Type</typeparam> /// <param name="action">return Method</param> /// <param name="timeSpan">out Check Method Run TimeSpan</param> /// <returns>Method return value</returns> public static T TimeCheck<T>(Func<T> action, out TimeSpan? timeSpan) { var stopwatch = Stopwatch.StartNew(); var res = action.Invoke(); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; ILog log = LogManager.GetLogger(action.Target.GetType()); log.Info(action.GetMethodInfo().Name + ": " + GetWorkingTime(stopwatch) + "(s)"); return res; } /// <summary> /// ex) TimeCheck(()=> Func<T>); /// <para> Return Func value && Write to Log.txt</para> /// </summary> /// <typeparam name="T">return Type</typeparam> /// <param name="action">return Method</param> /// <returns>Method return value</returns> public static T TimeCheck<T>(Func<T> action) { var stopwatch = Stopwatch.StartNew(); var res = action.Invoke(); stopwatch.Stop(); ILog log = LogManager.GetLogger(action.Target.GetType()); log.Info(action.GetMethodInfo().Name + ": " + GetWorkingTime(stopwatch) + "(s)"); return res; } /// <summary> /// ex) TimeCheck(action, out value); || TimeCheck(()=>action(x), out value); /// <para> Return timeSpan && Write to Log.txt</para> /// </summary> /// <param name="action">void Method</param> /// <param name="timeSpan">out Check Method Run TimeSpan</param> public static void TimeCheck(Action action, out TimeSpan? timeSpan) { var stopwatch = Stopwatch.StartNew(); action.Invoke(); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; ILog log = LogManager.GetLogger(action.Target.GetType()); log.Info(action.GetMethodInfo().Name + ": " + GetWorkingTime(stopwatch) + "(s)"); } /// <summary> /// ex) TimeCheck(action); || TimeCheck(()=>action(x)); /// <para>Write to Log.txt</para> /// </summary> /// <param name="action">void Method</param> public static void TimeCheck(Action action) { var stopwatch = Stopwatch.StartNew(); action.Invoke(); stopwatch.Stop(); ILog log = LogManager.GetLogger(action.Target.GetType()); log.Info(action.GetMethodInfo().Name + ": " + GetWorkingTime(stopwatch) + "(s)"); } public static string GetWorkingTime(Stopwatch stopwatch) { //TimeCheckMode(); TimeSpan spentTime = TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds); //return Math.Round(spentTime.TotalSeconds, 2).ToString("0.00"); return spentTime.TotalSeconds.ToString("0.00"); } #endregion ByMethod }
https://codereview.stackexchange.com/questions/195823/measuring-method-execution-time
728x90'C# > 기초' 카테고리의 다른 글
[C#] get=> , get; (0) 2024.02.14 [C#] Instance is Type (0) 2024.02.02 [C#] Logger (0) 2024.01.23 [C#] Interface (0) 2024.01.16 [C#] SuppressMessage (0) 2024.01.16 댓글