Gonzalo Contento
Gonzalo Contento

Reputation: 857

PostSharp OnMethodBoundaryAspect Indent/Padding

I've been trying to add some padding to my own Method Boundary Aspect.

StackTrace seems to be of no help, I cannot find a pattern to calculate the frame count baseline, which for simple scenarios is around 12.

[Serializable]
public class MyLogger : OnMethodBoundaryAspect
  public override void OnEntry(MethodExecutionArgs args)
  {
    // ...
    var ignoreFrameCount = ???; // in simple scenarios is ~12
    var n = new StackTrace().FrameCount - ignoreFrameCount;
    var padding = String.Empty.PadLeft(n*2);
    // ...

I know PostSharp does it (Indentation in Logging)

Is there anyway to accomplish this?

Upvotes: 1

Views: 159

Answers (1)

Daniel Balas
Daniel Balas

Reputation: 1850

Creating a StackTrace is generally really really slow and I would not certainly recommend it during logging.

The easiest way is to declare:

[ThreadStatic]
private static int indentLevel;

Then, all you need to do is to properly increment and decrement this variable in OnEntry and OnExit advices.

Upvotes: 1

Related Questions