使用 Unity\T4\anything 进行面向方面的日志记录

发布于 2024-09-16 19:06:15 字数 347 浏览 3 评论 0原文

在我的应用程序中,我们有一个跟踪记录器。我们在大多数重要方法的开头和结尾添加了日志语句,用于跟踪方法名称和参数值。现在这些跟踪语句使代码变得臃肿,阅读它们有点痛苦。

我正在考虑如何将代码的这方面与我的业务逻辑分开。

今天在读Unity的拦截框架。我突然想到是否可以使用通用记录器拦截我的方法调用并记录方法名称和参数值。我不确定是否可以使用反射读取方法参数。 Unity可以这样用吗?

另一个想法是运行 T4 代码生成引擎,在用特定属性修饰的所有方法的开头和结尾生成日志记录语句。由于我对T4知之甚少,有谁知道这是否可以实现?

还有其他方法可以将日志记录代码与我的业务逻辑分开吗?

干杯, 取消网格划分

In my application we have a trace logger. We have log statements added at the beginning and end of most of the important methods tracing the method name and the parameter values. Now these trace statements are bloating the code and it is a bit of a pain to read through them.

I am considering how can I separate this aspect of the code from my business logic.

Today I was reading about Unity's interception framework. I had a passing thought if it is possible to intercept my method calls with a generic logger and log the method name and parameter values. I am not sure if it is possible to read method parameters using reflection. Can Unity be used like this?

Another idea was to run the T4 code generation engine to generate logging statements at the beginning and end of all methods decorated with a particular attribute. Since I know very little about T4, does anyone know if this can be accomplished?

Are there any other ways to separate the logging code from my business logic?

Cheers,
Unmesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-09-23 19:06:15

是的,您可以使用 Unity,但您应该使用 Unity“拦截扩展”。 这里有一篇好文章

Yes, You can use Unity but you should user Unity "Interception extension". nice article here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文