How to wrap each function in project?

◇◆丶佛笑我妖孽 提交于 2019-12-24 01:54:12

问题


I have tons of functions in my WinForms project and i'm using NLog for logging. I want to have an ability to wrap each function with nlogger.Info("start") and nlogger.Info("end") so i will know in which place actual exception occurred or where the code currently runs.

Is there a smart way to do it or i will need to place lines from above in all of my functions ?


回答1:


I believe that PostSharp can do this for you, and can apply at the assembly level (via assembly-level attributes). However, I would be cautious of the potential performance impact. Writing the "aspect" should be pretty minimal.




回答2:


Ifyou don't want to do it manually - you could consider Aspect Oriented Programming.

To summarise what it is, it matches functions which run and offers 'advice' before, after or around. So you can easily make one which runs at 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 info:

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



来源:https://stackoverflow.com/questions/8200273/how-to-wrap-each-function-in-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!