GithubHelp home page GithubHelp logo

methodtimer's Introduction

Chat on Gitter NuGet Status

This is an add-in for Fody

Icon

Injects some very basic method timing code.

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

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);
        }
    }
}

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.