我正在寫一個非常簡單的實用程序類,目的是測量任何傳入的方法(任何類型)所執行的時間。如何計算實用程序類別的方法已過去的時間
在我的情況下Membership.ValidateUser(model.UserName, model.Password)
返回布爾,所以我得到一個異常。
我想如果可能的話寫一個這種類型的實用程序類的代碼示例如何解決它。 它是否有意義使用動態而不是行動?
Tracing.Log(Membership.ValidateUser(model.UserName, model.Password), "Membership.ValidateUser");
public static class Tracing
{
public static void Log(Action action, string message)
{
// Default details for the Log
string sSource = "TRACE";
string sLog = "Application";
// Create the Log
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
// Measure time Elapsed for an Action
Stopwatch stopwatch = Stopwatch.StartNew();
action();
stopwatch.Stop();
TimeSpan timeElapsed = stopwatch.Elapsed;
// Write the Log
EventLog.WriteEntry(sSource, "TIME-ELAPSED: " + timeElapsed .ToString() + message, EventLogEntryType.Warning, 234);
}
}
我想感謝您的解釋。怎麼可能重寫我的課程,並在內部使用labda?感謝您的時間 – GibboK 2013-03-13 12:38:53
@GibboK:我不知道你的意思。我已經向您展示瞭如何調用該方法。 – 2013-03-13 12:41:08
感謝Jon,我不知道MVC-Profiler。 – GibboK 2013-03-13 12:44:27