Skip to content

Injects some very basic method timing code.

License

Notifications You must be signed in to change notification settings

SkyKick/MethodTimer

 
 

Repository files navigation

This is an add-in for Fody

Icon

Injects some very basic method timing code.

Introduction to Fody

The nuget package NuGet Status

https://nuget.org/packages/MethodTimer.Fody/

PM> Install-Package MethodTimer.Fody

Your Code

public class MyClass
{
	[Time]
	public void MyMethod()
	{
		//Some code u are curious how long it takes
		Console.WriteLine("Hello");
	}
}

What gets compiled without an Interceptor

public class MyClass
{
    public void MyMethod()
    {
        var stopwatch = Stopwatch.StartNew();
        try
        {
            //Some code u are curious how long it takes
            Console.WriteLine("Hello");
        }
        finally
        {
            stopwatch.Stop();
            Debug.WriteLine("MyClass.MyMethod " + stopwatch.ElapsedMilliseconds + "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 the following form.

public static class MethodTimeLogger
{
    public static void Log(MethodBase methodBase, long milliseconds)
    {
        //Do some logging here
    }
}

Then this will be compiled

public class MyClass
{
    public void MyMethod()
    {
        var stopwatch = Stopwatch.StartNew();
        try
        {
            //Some code u are curious how long it takes
            Console.WriteLine("Hello");
        }
        finally
        {
            stopwatch.Stop();
            MethodTimeLogger.Log(methodof(MyClass.MyMethod), stopwatch.ElapsedMilliseconds);
        }
    }
}

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)]
internal class TimeAttribute : 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

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 96.1%
  • PostScript 3.9%