问题
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