title: Use C# attribute to track function call, variables, and return value?
body: <p>In Python, I can use decorators to trace the function call, its variables and return values.
  It is very easy to use.
  I just wonder can C# do the same thing?</p>
  
  <p>I find out there is a sample code of CallTracing Attribute online.
  However, it didn't show the result I expected.</p>
  
  <p>Does C# attributes have similar concepts as python's decorator?</p>
  
  <p>Thanks you
  and Best Regards!</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Method | AttributeTargets.ReturnValue |
      AttributeTargets.Property, AllowMultiple = false)]
  public class CallTracingAttribute : Attribute
  {
      public CallTracingAttribute()
      {
  
          try
          {
              StackTrace stackTrace = new StackTrace();
              StackFrame stackFrame = stackTrace.GetFrame(1);                
  
              Trace.TraceInformation("{0}-&gt;{1} {2}:{3}",
                  stackFrame.GetMethod().ReflectedType.Name,
                  stackFrame.GetMethod().Name,
                  stackFrame.GetFileName(),
                  stackFrame.GetFileLineNumber());
  
              Debug.WriteLine(string.Format("{0}-&gt;{1} {2}:{3}",
                  stackFrame.GetMethod().ReflectedType.Name,
                  stackFrame.GetMethod().Name,
                  stackFrame.GetFileName(),
                  stackFrame.GetFileLineNumber()));
          }
          catch
          {
          }
      }
  }
  
  class Program
  {
      [CallTracing]
      static int Test(int a)
      {
          return 0;
      }
  
      [CallTracing]
      static void Main(string[] args)
      {
          Test(1);
      }
  }
  </code></pre>