How to wrap each function in a project?

I have many features in my WinForms project and I use NLog for logging. I want to be able to wrap each function with nlogger.Info("start") and nlogger.Info("end") , so I will find out where the actual exception occurred or where the code is currently executing.

Is there any reasonable way to do this, or will I need to place the lines on top in all my functions?

+2
source share
2 answers

I believe that PostSharp can do this for you and can be applied at the assembly level (via build-level attributes). However, I would be careful about the potential performance impact. The spelling of the “aspect” should be fairly minimal.

+3
source

If you do not want to do this manually, you can consider aspect-oriented programming.

To summarize what it is, it corresponds to the functions that are launched and offer "advice" before, after or around. Thus, you can easily create one that works with all functions and adds to the log. Never used it for C #, but I used it for Java.

You can find a class for .NET. Read this for more information:

http://www.developerfusion.com/article/5307/aspect-oriented-programming-using-net/

+2
source

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


All Articles