Last active
August 29, 2015 14:24
-
-
Save davidbreyer/fea59b42b9dc5198dd6d to your computer and use it in GitHub Desktop.
Unity Interceptor to log the elapsed time of a method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProfilingInterceptionBehavior : IInterceptionBehavior | |
{ | |
[Dependency] | |
public ILog Log { get; set; } | |
public IMethodReturn Invoke(IMethodInvocation input, | |
GetNextInterceptionBehaviorDelegate getNext) | |
{ | |
//This behavior will record the start and stop time | |
//of a method and will log the method name and elapsed time. | |
//Get the current time. | |
var startTime = DateTime.Now; | |
//Log the start time of the method. | |
//This could be ommitted if you just want to see the response times of a method. | |
WriteLog(String.Format( | |
"Invoking method {0} at {1}", | |
input.MethodBase, startTime.ToLongTimeString())); | |
// Invoke the next behavior in the chain. | |
var result = getNext()(input, getNext); | |
//Calculate the elapsed time. | |
var endTime = DateTime.Now; | |
var timeSpan = endTime - startTime; | |
//The following will log the method name and elapsed time. | |
if (result.Exception != null) | |
{ | |
//Method threw an exception. | |
WriteLog(String.Format( | |
"Method {0} threw exception {1} at {2}. Elapsed Time: {3} ms", | |
input.MethodBase, result.Exception.Message, | |
endTime.ToLongTimeString(), | |
timeSpan.TotalMilliseconds)); | |
} | |
else | |
{ | |
//Method completed normally. | |
WriteLog(String.Format( | |
"Method {0} returned {1} at {2}. Elapsed Time: {3} ms", | |
input.MethodBase, result.ReturnValue, | |
endTime.ToLongTimeString(), | |
timeSpan.TotalMilliseconds)); | |
} | |
return result; | |
} | |
public IEnumerable<Type> GetRequiredInterfaces() | |
{ | |
return Type.EmptyTypes; | |
} | |
public bool WillExecute | |
{ | |
get { return true; } | |
} | |
private void WriteLog(string message) | |
{ | |
if (Log != null) | |
{ | |
Log.DebugFormat("Profiler: {0}", message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment