views:

228

answers:

3

I wanted to write a Visual Studio Macro or something similar which can fetch function name and insert into preset location in the error report part. It's clearer if you look at the example

Class SampleClass
{
    public void FunctionA()
    {
        try
        {
             //Do some work here
        }
        catch (Exception ex)
        {
          Logger.WriteLine(LogLevelEnum.Error, "SampleClass", "FunctionA Failed");
          Logger.WriteLine(LogLevelEnum.Error, "FunctionA", ex.ToString());
        }
        finally
        {
        }
    }
}

So, I followed the similar pattern of most of the critical functions of my common library. I would like to be able to insert "FunctionA" into the logging section during pre-built so that I don't have to remember to type in the right name or forgetting to rename it after copy and paste. Either that can be invoke manually from the toolbar or via shortcut key.

So, where should I start?

NOTE: I'm considered intermediate in .Net, been writting in C# and VB for more than 3 years, but I'm fresh on Macro, don't mind to learn though. Don't worry about the code itself and the exception, this is just an example.

EDIT: Thanks Ovidiu Pacurar and cfeduke. What I wanted here was a one off way to change-and-forget. PostSharp will incur overhead on every one of those function, even when exception is not thrown. Digging from the stacktrace is feasible, but at some point I would also like to just log "FunctionA Failed" without spending too much processing in getting the stacktrace. Further more, if the library is obfuscated, the stacktrace would be cryptic.

Actually there was another need for this feature, which I forgot to mention earlier. When the library is ready to be delivered, I would want to change all the function name into function code, "FunctionA" might be "0001", by referring to a table, so as to solve the "obfuscated" log problem.

+1  A: 

Take a look at System.Diagnostics.StackTrace and then you can create just one log call getting the function from the stack.

Ovidiu Pacurar
A: 

Look at PostSharp. It allows you do this with very easy way and much more.

There is samples with loging too.

TcKs
+1  A: 

I wish C# had __FILE__ and __LINE__-like macros but it doesn't. You can, however, post-compile process C# using PostSharp. This has the advantage of not having to invoke the overhead of a stack trace to get a method name at runtime. The performance overhead may not be something you are concerned about during an exception handler, but in any case PostSharp is another tool which is available that can perform the job.

The example video for PostSharp does something similar to what you are attempting to do. Take a look at this sample code right off the front page of the PostSharp site to get your gears turning:

public  class SimplestTraceAttribute : OnMethodBoundaryAspect
{
  public override void OnEntry( MethodExecutionEventArgs eventArgs)
  {
    Trace.TraceInformation("Entering {0}.", eventArgs.Method);
    Trace.Indent();
  }
  public override void OnExit( MethodExecutionEventArgs eventArgs)
  {
    Trace.Unindent();
    Trace.TraceInformation("Leaving {0}.", eventArgs.Method);
  }
}
cfeduke