Injects some very basic method timing code.
C#
Clone or download
Latest commit 1169f77 Aug 12, 2018
Permalink
Failed to load latest commit information.
.github fix assembly version Feb 8, 2018
AssemblyToReference update to new fody packaging Jan 14, 2018
AssemblyWithAttributeOnAssembly update to new fody packaging Jan 14, 2018
AssemblyWithAttributeOnModule update to new fody packaging Jan 14, 2018
AssemblyWithInterceptor update to new fody packaging Jan 14, 2018
AssemblyWithInterceptorAndFormatting update to new fody packaging Jan 14, 2018
AssemblyWithInterceptorAndFormattingWithWrongParameters update to new fody packaging Jan 14, 2018
AssemblyWithInterceptorAndFormattingWithoutOverload update to new fody packaging Jan 14, 2018
AssemblyWithInterceptorInReference update to new fody packaging Jan 14, 2018
AssemblyWithNopInterceptor update to new fody packaging Jan 14, 2018
AssemblyWithTimeSpanInterceptorAndFormatting Add support for TimeSpan overloads in interceptor Jul 20, 2018
AssemblyWithoutInterceptor update to new fody packaging Jan 14, 2018
MethodTimer.Fody cleanup Jul 28, 2018
MethodTimer update to fody 3.1.3 Jun 24, 2018
Tests fix release build Jul 29, 2018
.editorconfig GitHubSync update Aug 12, 2018
.gitattributes Update .gitattributes Mar 23, 2013
.gitignore . Nov 17, 2017
Directory.Build.props update version Jul 29, 2018
MethodTimer.sln Add support for TimeSpan overloads in interceptor Jul 20, 2018
MethodTimer.sln.DotSettings update to new fody packaging Jan 14, 2018
README.md doco Jul 29, 2018
appveyor.yml GitHubSync update Aug 12, 2018
license.txt license Sep 24, 2012
package_icon.png . Nov 17, 2017

README.md

Chat on Gitter NuGet Status

This is an add-in for Fody

Icon

Injects some very basic method timing code.

Introduction to Fody

Usage

See also Fody usage.

NuGet installation

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

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

The Update-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

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <MethodTimer/>
</Weavers>

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();
            Trace.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 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)

public static class MethodTimeLogger
{
    public static void Log(MethodBase methodBase, long milliseconds, string message)
    {
        //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);
        }
    }
}

Interceptor with elapsed duration as TimeSpan

public static class MethodTimeLogger
{
    public static void Log(MethodBase methodBase, TimeSpan elapsed, string message)
    {
        //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.Elapsed);
        }
    }
}

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.

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

Then this will be compiled

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

Note that 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)]
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