跟踪与日志记录以及 log4net 如何适应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/172372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tracing versus Logging and how does log4net fit in?
提问by Xerx
I am wondering about what the difference between logging and tracing is.
我想知道日志记录和跟踪之间的区别是什么。
Is the difference basically that tracing is more detailed log giving developers a tool to debug applications at runtime?
区别基本上是跟踪是更详细的日志,为开发人员提供了在运行时调试应用程序的工具?
I have been experimenting with log4net and doing logging. Now I am wondering if I should be doing tracing as well and if I could/should use log4net for that purpose. Should I be doing tracing with log4net and is there some trace level for log4net loggers? Should I use a different log level for debug and trace purposes or is it ok to use the same? Can you give a simple example on how I would do logging and tracing for a simple method?
我一直在试验 log4net 并进行日志记录。现在我想知道我是否也应该进行跟踪,以及我是否可以/应该为此使用 log4net。我应该使用 log4net 进行跟踪吗?log4net 记录器是否有一些跟踪级别?我应该使用不同的日志级别进行调试和跟踪,还是可以使用相同的级别?你能举一个简单的例子来说明我将如何为一个简单的方法进行日志记录和跟踪吗?
Edit: Despite a few helpful answers below I am still unsure how I should be doing tracing versus logging.
编辑:尽管下面有一些有用的答案,但我仍然不确定我应该如何进行跟踪与日志记录。
I have the following method in my Business layer and I want to add logging/tracing to it. I am wondering how to do it efficiently. Is the following method acceptable in terms of logging/tracing? Should the log messages be of type Info instead of Debug? Are the Debug messages I am logging considered trace? How would you change it?
我的业务层中有以下方法,我想向其中添加日志记录/跟踪。我想知道如何有效地做到这一点。以下方法在日志记录/跟踪方面是否可以接受?日志消息的类型应该是 Info 而不是 Debug 吗?我记录的调试消息是否被视为跟踪?你会如何改变它?
IEnumerable<Car> GetCars()
{
try
{
logger.Debug("Getting cars");
IEnumerable<Car> cars = CarAccessor.GetCars().ConvertAll(DataAccessToBusinessConverter);
logger.Debug("Got total of " + cars.Count + " cars");
} catch (Exception e) {
logger.Error("Error when getting cars", e);
throw new Exception("Unexpected error when getting cars");
}
}
回答by martin
Logging is the generic term for recording information - tracing is the specific form of logging used to debug.
日志记录是记录信息的通用术语 - 跟踪是用于调试的特定日志记录形式。
In .NET the System.Diagnostics.Trace and System.Diagnostics.Debug objects allow simple logging to a number of "event listeners" that you can configure in app.config. You can also use TraceSwitches to configure and filter (between errors and info levels, for instance).
在 .NET 中,System.Diagnostics.Trace 和 System.Diagnostics.Debug 对象允许简单记录到许多可以在 app.config 中配置的“事件侦听器”。您还可以使用 TraceSwitches 来配置和过滤(例如,在错误和信息级别之间)。
private void TestMethod(string x)
{
if(x.Length> 10)
{
Trace.Write("String was " + x.Length);
throw new ArgumentException("String too long");
}
}
In ASP.NET, there is a special version of Trace (System.Web.TraceContext) will writes to the bottom of the ASP page or Trace.axd. In ASP.NET 2+, there is also a fuller logging framework called Health Monitoring.
在 ASP.NET 中,有一个特殊版本的 Trace(System.Web.TraceContext)将写入 ASP 页面或 Trace.axd 的底部。在 ASP.NET 2+ 中,还有一个更完整的日志记录框架,称为 Health Monitoring。
Log4Net is a richer and more flexible way of tracing or logging than the in-built Trace, or even ASP Health Monitoring. Like Diagnostics.Trace you configure event listeners ("appenders") in config. For simple tracing, the use is simple like the inbuilt Trace. The decision to use Log4Net is whether you have more complicated requirements.
Log4Net 是一种比内置 Trace 甚至 ASP Health Monitoring 更丰富、更灵活的跟踪或日志记录方式。像 Diagnostics.Trace 一样,您可以在配置中配置事件侦听器(“附加程序”)。对于简单的跟踪,使用就像内置的 Trace 一样简单。使用 Log4Net 的决定是您是否有更复杂的需求。
private void TestMethod(string x)
{
Log.Info("String length is " + x.Length);
if(x.Length> 10)
{
Log.Error("String was " + x.Length);
throw new ArgumentException("String too long");
}
}
回答by Kevin Little
IMO...
Logging should notbe designed for development debugging (but it inevitably gets used that way)
Logging should be designed for operationalmonitoring and trouble-shooting -- this is its raison d'être.
Tracing should be designed for development debugging & performance tuning. If available in the field, it can be use for really low-level operational trouble-shooting, but that is not its main purpose
Given this, the most successful approaches I've seen (and designed/implemented) in the past do notcombine the two together. Better to keep the two tools separate, each doing one job as well as possible.
IMO...
日志不应该是为开发调试而设计的(但它不可避免地会被这样使用)
日志应该是为操作监控和故障排除而设计的——这是它存在的理由。
跟踪应该是为开发调试和性能调优而设计的。如果在现场可用,它可以用于真正低级别的操作故障排除,但这不是它的主要目的
鉴于此,我过去见过(和设计/实施)的最成功的方法不结合两人在一起。最好将这两个工具分开,每个工具都尽可能做好一项工作。
回答by Bob Nadler
log4net is well suited for both. We differentiate between logging that's useful for post-release diagnostics and "tracing" for development purposes by using the DEBUG logging level. Specifically, developers log their tracing output (things that are only of interest during development) using Debug(). Our development configuration sets the Level to DEBUG:
log4net 非常适合两者。我们通过使用 DEBUG 日志记录级别来区分对发布后诊断有用的日志记录和用于开发目的的“跟踪”。具体来说,开发人员使用Debug(). 我们的开发配置将 Level 设置为 DEBUG:
<root>
<level value="DEBUG" />
...
</root>
Before the product is released, the level is changed to "INFO":
在产品发布之前,级别更改为“INFO”:
<level value="INFO" />
This removes all DEBUG output from the release logging but keeps INFO/WARN/ERROR.
这会从发布日志中删除所有 DEBUG 输出,但保留 INFO/WARN/ERROR。
There are other log4net tools, like filters, hierarchical (by namespace) logging, multiple targets, etc., by we've found the above simple method quite effective.
还有其他 log4net 工具,如过滤器、分层(按命名空间)日志记录、多个目标等,我们发现上述简单方法非常有效。
回答by Alois Kraus
Logging is not Tracing. These two should be different libraries with different performance characteristics. In fact I have written one tracing libraryby myself with the unique property that it can trace automatically the exception when the method with tracing enabled is left with an exception. Besides this it is possible to resolve in an elegant waythe problem to trigger exceptions in specific places in your code.
日志记录不是跟踪。这两个应该是具有不同性能特征的不同库。事实上,我自己编写了一个跟踪库,它具有独特的属性,当启用跟踪的方法出现异常时,它可以自动跟踪异常。除此之外,还可以以优雅的方式解决在代码中的特定位置触发异常的问题。
回答by gbjbaanb
I'd say yes. Logging is the only way to determine what happened in the past - if a customer calls and says something didn't happen as expected, without a log all you can do is shrug and try and reproduce the error. Sometimes that is impossible (depending on the complexity of the software and the reliance on customer data).
我会说是的。日志记录是确定过去发生的事情的唯一方法 - 如果客户打电话说事情没有按预期发生,没有日志,您所能做的就是耸耸肩并尝试重现错误。有时这是不可能的(取决于软件的复杂性和对客户数据的依赖)。
There is also the question of logging for auditing, a log file can be written containing information on what the user is doing - so you can use that to narrow down the possibilities to debug a problem, or even to verify the user's claims (if you get a report that the system is broken, xyz didn't happen, you can look in the logs to find out the operator failed to start the process, or didn't click the right option to make it work)
还有一个用于审计的日志记录问题,可以编写一个日志文件,其中包含有关用户正在做什么的信息 - 因此您可以使用它来缩小调试问题的可能性,甚至验证用户的声明(如果您得到系统损坏的报告,xyz 没有发生,您可以查看日志以找出操作员未能启动该过程,或者没有单击正确的选项使其工作)
Then there's logging for reporting, which is what most people think logging is for.
然后是用于报告的日志记录,这是大多数人认为日志记录的目的。
If you can tailor the log output then put everything in logs and reduce or increase the amount of data that gets written. If you can change the output level dynamically then that's perfect.
如果您可以定制日志输出,那么将所有内容都放入日志并减少或增加写入的数据量。如果您可以动态更改输出电平,那就完美了。
You can use any means of writing logs, subject to performance issues. I find appending to a text file is the best, most portable, easiest to view, and (very importantly) easiest to retrieve when you need it.
您可以使用任何写入日志的方式,但会受到性能问题的影响。我发现附加到文本文件是最好的、最便携的、最容易查看的,并且(非常重要的是)在您需要时最容易检索。
回答by Karl
logging != debugging
记录 != 调试
Sometimes keeping log files is necessary to solve issues with the client, they prove what happened on the server side.
有时保留日志文件是解决客户端问题所必需的,它们可以证明服务器端发生了什么。
回答by Christian.K
Also, consider what information is logged or traced. This is especially true for senstive information.
此外,请考虑记录或跟踪哪些信息。对于敏感信息尤其如此。
For example, while it may be generally OK to log an error stating
例如,虽然通常可以记录错误说明
"User 'X' attempted to access but was rejected because of a wrong password",
“用户‘X’试图访问,但由于密码错误而被拒绝”,
it is not OK to log an error stating
记录错误说明是不行的
"User 'X' attempted to access but was rejected because the password 'secret' is not correct."
“用户‘X’尝试访问但被拒绝,因为密码‘secret’不正确。”
It mightbe acceptable to write such senstive information to a trace file (and warn the customer/user about that fact by "some means" before you ask him to enable trace for extended troubleshooting in production). However for logging, I always have it as a policy that such senstive information is neverto be written (i.e. levels INFO and above in log4net speak).
这可能是可以接受的写这样灵敏的信息来跟踪文件(你问他能在生产中扩展故障跟踪之前通过“某种方式”警告这一事实客户/用户)。但是,对于日志记录,我始终将永远不会写入此类敏感信息作为策略(即 log4net 中的级别 INFO 及以上级别)。
This must be enforced and checked by code reviews, of course.
当然,这必须由代码来执行和检查。

