views:

15

answers:

1

Hi!

I plan to put a call to MethodBase.GetCurrentMethod() at the beginning of most methods (for informative logging), but since this would present a high overhead, it would be good if a conditional attribute could be used used like:

#define LogMethodNames

where...

[Conditional("LogMethodNames")]

was put above every line call GetCurrentMethod(), like:

void DoStuff()
{
    [Conditional("LogMethodNames")]
    logger.CurrentMethod = MethodBase.GetCurrentMethod();

    //  stuff done here
}

...so at least it could be excluded from release builds.

Is something along these lines possible?

Thanks!

Gregg

PS I would try this myself now but for some reason I couldn't get Attributes to work due to a compile error. Doh.

PPS If this can't work I'd create a method which assigns to the logger object, like:

void SetCurrentMethod(MethodBase currentMethod)
{
    logger.CurrentMethod = currentMethod;
}

and use:

void DoStuff()
{
    [Conditional("LogMethodNames")]
    SetCurrentMethod(MethodBase.GetCurrentMethod());

    //  stuff done here
}

Any thoughts?! Thanks :-)

+1  A: 

You are close - you would need to set the Conditional attribute on the method itself:

[Conditional("LogMethodNames")]
void SetCurrentMethod(MethodBase currentMethod)
{
    logger.CurrentMethod = currentMethod;
}

For more information please see The Conditional attribute:

The attribute Conditional enables the definition of conditional methods. The Conditional attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol is defined, the call is included; otherwise, the call (including evaluation of the parameters of the call) is omitted.

Andrew Hare
Thx. I need to broaden my thinking, lol. Good luck with 50K, .1 to go...
Gregg Cleland