记录-改变实施?
我们一直在使用 log4net 来记录我们的 ASP.NET Web 表单应用程序。我们的日志记录通常位于业务层,典型的实现是这样的。
SomeMethodCall(MethodParams)
{
Log.Start("Starting Some Method");
try
{
//do something
}
catch(Exception ex)
{
Log.Exception("exception in SomeMethodCall" + ex.message);
}
Log.End("End SomeMethod");
}
在我看来,它有点笨拙。有没有一种不使用 AOP 的更干净的方法?我不确定我是否需要添加一个框架的开销,只是为了日志记录,我认为它会给我很多其他选择(我这样做)不需要)
我正在考虑使用一些 AOP 框架以更简洁的方式来完成它,只需用属性标记方法来记录和处理异常。
我对 AOP 有两件事关心(在我最初阅读之后)。
有些框架将代码注入到您的 IL 中(根据我的理解),并且担心它是否会误导我。我可能正在查看由我的 AOP 框架给出的 x 行,而实际上它可能是我的应用程序中的 y 行。我的恐惧是没有根据的吗?
性能:如果使用 AOP 框架,会增加多少性能开销。
编辑:我也在研究 PolicyInjectionApplicationBlock 。不幸的是,我没有能力改变我的业务逻辑中的实现
We have been using log4net for logging our asp.net web forms application. Our logging is typically in our business layer and the typical implementation is like this
SomeMethodCall(MethodParams)
{
Log.Start("Starting Some Method");
try
{
//do something
}
catch(Exception ex)
{
Log.Exception("exception in SomeMethodCall" + ex.message);
}
Log.End("End SomeMethod");
}
In my opinon, it is a bit clumsy. Is there a cleaner way of doing it without using AOP ?I am not sure if I would need to have the overhead of adding a framework, just for logging, thought I understand that it would give me a lot of other options (which I do not need)
I am thinking of using some AOP frameworks to do it in a more cleaner way, just by marking the methods with attributes to log and handle exceptions.
There are 2 things I am concerned with AOP (after my initial reading).
Some frameworks inject code into your IL(as per my understanding) and am concerned if it would misguide me. I might be looking at line x, given by my AOP framework, where as it actually might be line y in my application. Is my fear unfounded?
Performance: How much of a performance overhead would be added if using an AOP framework.
Edit: I was also looking into PolicyInjectionApplicationBlock. Unfortunately, I do not have the luxury of changing implementaions inside my business logic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是说您的大多数/所有方法都采用这种结构吗?如果是这样,我的建议是减少你的日志记录。 (我强调这是建议,也可能是有争议的建议)。
实际上并不需要以这种方式使用每个方法的进入和退出的一揽子日志,而是更有策略地放置日志条目。我喜欢确保所有日志消息都有一个“目的” - 如果我想不出该日志消息在调试时会以某种方式帮助我的场景,那么它就没有必要出现在我的代码中!
Are you saying that most / all of your methods take this structure? If so, my advice would be to tone down your logging. (I stress that this is advice, and possibly controversial advice as well).
There shouldn't really be a need to use a blanket log of the entry and exit of every single method in this way, instead place your log entries more strategically. I like to ensure that all log messages have a "purpose" - if I can't think of a scenario where that log message would help or aid me in some way while debugging then it has no business being in my code!
您可以阅读 这,另请阅读.Net中AOP的7种方法,我在Java中使用AOP并且没有看到性能问题。无论如何检查这里要小心...。
附加
Spring.Net AOP
You can read this, also read 7 approaches for AOP in .Net, I use AOP in Java and i didn't see performance problem. Anyway check here to be careful....
Additional
Spring.Net AOP