Skip to content

Latest commit

History

1,084 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

MethodTimer.Fody

NuGet Status

Injects some very basic method timing code.

The implementation uses an allocation-free method to determine the duration of a method.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the MethodTimer.Fody NuGet package and update the Fody NuGet package:

PM>Install-Package Fody
PM>Install-Package MethodTimer.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <MethodTimer/> to FodyWeavers.xml

<Weavers>
<MethodTimer/>
</Weavers>

Your Code

publicclassMyClass{[Time]publicvoidMyMethod(){//Some code u are curious how long it takesConsole.WriteLine("Hello");}}

What gets compiled without an Interceptor

publicclassMyClass{publicvoidMyMethod(){varstart=Stopwatch.GetTimestamp();try{//Some code u are curious how long it takesConsole.WriteLine("Hello");}finally{varend=Stopwatch.GetTimestamp();varelapsed=end-start;varelapsedTimeSpan=newTimeSpan(...);Trace.WriteLine("MyClass.MyMethod "+elapsedTimeSpan.TotalMilliseconds+"ms");}}}

What gets compiled with an Interceptor

If you want to handle the logging you can define a static class to intercept the logging.

The interceptor takes one of the two following forms.

Note: when both methods are available, the intercepter will prefer the TimeSpan overload.

Interceptor with elapsed duration as long (milliseconds)

publicstaticclassMethodTimeLogger{publicstaticvoidLog(MethodBasemethodBase,longmilliseconds,stringmessage){//Do some logging here}}

Then this will be compiled

publicclassMyClass{publicvoidMyMethod(){varstart=Stopwatch.GetTimestamp();try{//Some code u are curious how long it takesConsole.WriteLine("Hello");}finally{varend=Stopwatch.GetTimestamp();varelapsed=end-start;varelapsedTimeSpan=newTimeSpan(...);MethodTimeLogger.Log(methodof(MyClass.MyMethod),(long)elapsedTimeSpan.TotalMilliseconds);}}}

Interceptor with elapsed duration as TimeSpan

publicstaticclassMethodTimeLogger{publicstaticvoidLog(MethodBasemethodBase,TimeSpanelapsed,stringmessage){//Do some logging here}}

Then this will be compiled

publicclassMyClass{publicvoidMyMethod(){varstart=Stopwatch.GetTimestamp();try{//Some code u are curious how long it takesConsole.WriteLine("Hello");}finally{varend=Stopwatch.GetTimestamp();varelapsed=end-start;varelapsedTimeSpan=newTimeSpan(...);MethodTimeLogger.Log(methodof(MyClass.MyMethod),elapsedTimeSpan);}}}

Using parameters inside the logging

If you want to get the parameter values inside the logging, you can use a string format in the attribute definition.

publicclassMyClass{[Time("File name: '{fileName}'")]publicvoidMyMethod(stringfileName){//Some code u are curious how long it takesConsole.WriteLine("Hello");}}

Then this will be compiled

publicclassMyClass{publicvoidMyMethod(stringfileName){varstart=Stopwatch.GetTimestamp();try{//Some code u are curious how long it takesConsole.WriteLine("Hello");}finally{varend=Stopwatch.GetTimestamp();varelapsed=end-start;varelapsedTimeSpan=newTimeSpan(...);varmessage=string.Format("File name: '{0}'",fileName);MethodTimeLogger.Log(methodof(MyClass.MyMethod),(long)elapsedTimeSpan.TotalMilliseconds,message);}}}

The following values are allowed:

  • Any parameter name (e.g. {fileName})
  • {this} (calls ToString() on the instance itself) - Note that this is not available on static methods, the weaver will throw an error if being used in a static method

Note 1: sub-properties are not (yet?) supported. Support Fody on OpenCollective and this might be implemented!

Note 2: this feature requires an updated Log method call with the definition below. If this method (with the message parameter) is not found, the weaver will raise an error.

public static void Log(MethodBase methodBase, long milliseconds, string message)

Whats in the NuGet

In addition to the actual weaving assembly the NuGet package will also add a file TimeAttribute.cs to the target project.

[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Module|AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Constructor,AllowMultiple=false)]classTimeAttribute:Attribute{}

At compile time this attribute and all usages to it will be removed from the target assembly. If you want to re-use the class in a common assembly change the class from internal to public. This will result in the class not being removed at compile time.

Icon

Icon courtesy of The Noun Project

About

Injects some very basic method timing code.

Resources

Security policy

Stars

777 stars

Watchers

16 watching

Forks

Sponsor this project

Used by

Contributors

Languages