DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

A .NET Tracing Attribute

08.25.2009
| 3656 views |
  • submit to reddit
        // a simple way to trace method execution.
//

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, 
        AllowMultiple = false)]
    public class CallTracingAttribute : Attribute 
    {
        public CallTracingAttribute()
        {
#if TRACE
            try
            {
                StackTrace stackTrace = new StackTrace();
                StackFrame stackFrame = stackTrace.GetFrame(1);
                Trace.TraceInformation("{0}->{1} {2}:{3}",
                    stackFrame.GetMethod().ReflectedType.Name,
                    stackFrame.GetMethod().Name,
                    stackFrame.GetFileName(),
                    stackFrame.GetFileLineNumber());
            }
            catch
            {
            }
#endif
        }
    }