登录别人的代码

发布于 2024-09-27 14:46:27 字数 255 浏览 1 评论 0原文

我刚刚开始开发 C# 应用程序(使用 log4net)。我应该将日志添加到其他人编写的代码中。因此,我不可能每次都能很好地理解上下文(代码太多,时间太少:))。

我一直遵循一个看似相当粗糙的惯例。我在每个日志中显示日志级别、日期时间、类名、方法名。我在构造函数、某些事件和每个 catch 语句中打印进入和退出每个方法(其中大多数方法,我尝试排除大循环内的方法)的日志。

我认为我在某些地方做得太过分了,但需要一定程度的一致性。关于正确(或更好)方法的任何建议吗?

I have just begun working on a C# application (using log4net). I'm supposed to add logs to code written by someone else. Hence, it's not possible for me to understand well the context each time (too much code, too less time :) ).

I have been following a convention which seems quite crude. I display log level, datetime, class name, method name with every log. I print the log on entering and exiting every method (most of them, i try to exclude method within big loops), in constructors, some events and every catch statement.

I think I'm overdoing it at places but some degree of uniformity is required. Any suggestions on the right (or better) approach?

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

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

发布评论

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

评论(4

°如果伤别离去 2024-10-04 14:46:27

记录每个方法以及构造函数的开始和结束肯定是矫枉过正。

恐怕如果您不理解该方法,您将无法记录适当的问题。

至少记录所有异常,永远不要捕获异常而不对其进行处理。

Logging the start and end of every method, and the constructors, is certainly overkill.

I'm afraid that if you don't understand the method you won't be able to log the appropriate issues.

At the very least log all exceptions, never catch an exception without doing something with it.

谁对谁错谁最难过 2024-10-04 14:46:27

我建议,你首先找出现在需要日志的原因是什么。毕竟,您正在开发的代码显然是在没有太多日志记录的情况下开发的,那么他们希望您添加日志记录的原因是什么:他们在定位错误时是否遇到问题(什么样的错误?)他们希望能够在回顾时看到当程序做了某事时(这是什么东西?)?

I suggest, you first of all find out what is the reason why the log is needed now. After all, the code you are working on was apperntly developed without much logging, so what is the reason they want you to add logging: do they have problems locating bugs (what kind of bugs?) do they want be able to see in retrospect when the program did something (what is this something?)?

无人问我粥可暖 2024-10-04 14:46:27

从我的角度来看,可以在调试中记录每个函数/构造函数的输入参数和结果。
但请保持日志消息尽可能简短,因为大多数数据可以通过 log4net 配置添加/更改。因此仅记录 parameter1: value1parameter2: value2result:x

From my pov it is okay to log every function/constructor with input parameters and results in debug.
But keep the log messages as brief as possible as most data can be added/changed via the log4net configuration. Thus log only parameter1: value1 parameter2: value2 and result:x

时光磨忆 2024-10-04 14:46:27

也许您还应该考虑记录重要的条件语句及其分支(条目),例如 if/else if/else 和 switch/case/default 等。这将帮助您在方法内提供更多逻辑细节。

Maybe you should also consider to log important conditional statements and their branches (entries), such as if/else if/else, and switch/case/default etc. This will help you provide more logic details inside the methods.

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