Feedback

MethodStopwatch – timemeasuring class for methods

(this.Method != null) ? this.Method.Name : „“Couldn not resolve method““

using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;

namespace TimeMeasuring
{
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++
    // MethodStopwatch is a easy to use static class for measuring the time 
    // of a method (i.e. by simple calling "Start" at the begin of a method and "Stop" or "StopAndPublish" at the end 
    // by codeteq, 2011
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++

    public static class MethodStopwatch
    {

        #region private classes

        private class _MethodStopwatch : Stopwatch
        {

            #region consts, members, properties

            public System.Reflection.MethodBase Method
            {
                get
                {
                    try
                    {
                        StackFrame sf = new StackFrame(4);
                        return sf.GetMethod();
                    }
                    catch { return null; }
                }
            }

            #endregion

            #region .ctor

            public _MethodStopwatch(bool autostart = true)
            {
                if (autostart)
                    this.Start();
            }

            #endregion

            #region methods

            public string StopAndPublish(Stream stream, string suffix = null)
            {
                this.Stop();
                return Publish(stream, suffix);
            }

            public string Publish(Stream stream, string suffix)
            {
                string output = String.Format("Method: "{0}"" lasted: {1}ms {2}""

1 Kommentar