AOP (aspect-oriented programming) and logging. Is it really functional?

we are trying to implement logging in our application using AOP (and PostSharp, by the way, but this question applies to any AOP framework).

The problem we are facing is that the information we receive is this:

Entering method XXX with parameters:

  • if it is a value type.
  • anything in the ToString () override, if one is executed.
  • classname if ToString () is not overridden.

This information is not very useful, since usually we get the 3rd case. We also create a lot of useful information.

If you used AOP to log into any product, how did you deal with this problem?

Thanks in advance.

+6
source share
2 answers

Several approaches:

  • Place the common interface on the types you want to register. (For example, ILoggable). Implementing this interface will give your aspect the ability to register exactly what you want. The downside is that you have to implement / maintain ILoggable for every object that you could register.

  • Use reflection (this is what I did in this audit example on my blog. It uses MVC ActionFilter, but the principle is the same). The trade-offs are on the blog, but mostly the difficulty of using reflection and performance, depending on how much you register and how often.

  • Use serialization. Given the object, serialize it in Json or XML or whatever, and write this line down. Depending on what you do with the magazines, this can vary from perfect to worthless, and depending on how serialization works and how complex the objects are, this can be a performance issue.

+3
source

I am working on a new kind of AOP Framework to answer the missing features of the existing AOP Framework. You can find my open source project here: NConcern.NET AOP Framework

One of the differences from the others is that you can develop your advice using System.Linq.Expression to avoid boxing / unpacking, reflection and hash type jumps. It's a little harder to develop with Expression for beginners, but easy for an advanced developer.

An example of a simple example of logging (in the console) without overwriting your company, reflection and boxing.

business: calculator

public class Calculator { public int Add(int a, int b) { return a + b; } } 

your logging aspect, implemented with the linq expression, to describe how the Council should work.

 public class Logging : IAspect { //this is not an advice, this method is called to produce advices public IEnumerable<IAdvice> Advise(MethodInfo method) { //generic presentation method var presentation = typeof(Presentation). GetMethod("Of"); //example log on exception //instance, arguments and exception are Expressions yield return Advice.Linq.After.Throwing((instance, arguments, exception) => { Expression.Call ( typeof(Console).GetMethod("WriteLine",...), Expression.Call ( typeof(string).GetMethod("Concat", new Type[] { typeof(string[]) }), Expression.NewArrayInit(typeof(string), arguments.Select(argument => argument.Type == typeof(string) ? argument : ...).ToArray()) ) ) } } } 
0
source

Source: https://habr.com/ru/post/957410/


All Articles