Have you ever had the problem where your program misbehaves and you just do not have a debugger attached at the moment? Or that some bug is appearing occasionally and you just don't know how to reproduce such a case manually? The usual solution here is logging. But introducing logging into your code comes with a lot of manual work and adds a lot of boilerplate. There is also the dilemma of deciding which logic should be traced and at which level of detail.
All this pain is taken away with PostSharp.
When you apply the
[LogMethod] aspect to a method, the method creates a log record when:
Additionally, you can apply the '[LogSetValue]` to a field or property to create a log record when the field or property is set.
The example uses the following PostSharp features:
OnMethodBoundaryAspectclass, the advices
OnExceptionand context values
LocationInterceptionAspectclass, the advice
OnSetValueand context value
This example is purely educational. It has the following limitations:
If you need production-grade logging, you should consider using the
LogAttribute aspect works out of the box. The
LogAttribute aspect is highly optimized for run-time performance, multithreading and reentrance.
You can set up logging in a few minutes without modifying your existing source code.