我正在使用 log4net 来记录对 API 的调用。很多电话。我调用的方法具有多个兆字节的请求/响应对数据,并且无论我使用哪种日志记录模式,都很难读取将多个调用写入同一文件的日志。所以,我认为最好的方法是记录到多个文件。
我很难弄清楚如何让 log4net 做到这一点,或者它是否支持它。
来自 Log4Net 常见问题解答 - 多个客户端请求的输出可以转到不同的日志文件吗?
许多开发人员都面临着区分来自同一类但不同客户端请求的日志输出的问题。他们提出了巧妙的机制将日志输出分散到不同的文件中。在大多数情况下,这不是正确的方法。
使用上下文属性或堆栈(ThreadContext)更简单...此后,日志输出将自动包含上下文数据,以便您可以区分来自不同客户端请求的日志,即使它们输出到同一个文件。< /p>
我查看了有关上下文和上下文属性的文档。事件上下文似乎最适合,但我也尝试阅读其他上下文的文档。看来它们只是允许我添加最终出现在日志文件中的附加属性,而不是成为日志文件名的组成部分,或者允许我自动附加到不同的文件。
有没有办法配置附加程序来为不同的上下文属性或上下文堆栈级别等创建不同的文件?
编辑:
我正在通过 Castle Windsor Logging 设施使用 log4net,并且我正在考虑切换到 NLog 来解决这个问题。
NLog 似乎通过在 File
目标的 fileName
属性中使用 {logger}
布局渲染器来支持此行为。我可以通过使用 Windsor 的 ILogger.CreateChildLogger
方法创建子记录器并设置 {logger.shortName=True}
来有效地设置此属性。
参阅:
http://nlog-project.org/forum#nabble-td1685989
请 如果可能的话,我仍然更喜欢使用 log4net,因为我正在测试的项目使用它。也许我的 NLog 示例可以给某人带来如何在 log4net 上完成此操作的灵感,也许他们可以帮助我弄清楚:)
I am using log4net for logging calls to an API. Many calls. The methods I am calling have multiple megabytes of data for request/response pairs, and it is very hard to read logs that have multiple calls written to the same file, no matter what logging pattern I use. So, I feel the best approach is to log to multiple files.
I am having a hard time figuring out how to get log4net to do this, or if it even supports it.
From the Log4Net FAQ - Can the outputs of multiple client request go to different log files?
Many developers are confronted with the problem of distinguishing the log output originating from the same class but different client requests. They come up with ingenious mechanisms to fan out the log output to different files. In most cases, this is not the right approach.
It is simpler to use a context property or stack (ThreadContext) ... Thereafter, log output will automatically include the context data so that you can distinguish logs from different client requests even if they are output to the same file.
I looked at the documentation on Contexts and Context Properties. It seemed Event Context fit best, but I tried reading docs for other Contexts too. It seems they just allow me to put additional properties that end up in my log files, rather than being a component of a log file name, or allow me to automatically append to different files.
Is there a way to configure appenders to create different files for different context properties or context stack levels, etc?
Edit:
I am using log4net via Castle Windsor Logging facility, and I'm considering switching to NLog to solve this problem.
NLog seems to support this behavior by using the {logger}
layout renderer in the File
target's fileName
property. I can effectively set this property by making a child logger with Windsor's ILogger.CreateChildLogger
method, and setting {logger.shortName=True}
.
See:
http://nlog-project.org/forum#nabble-td1685989
I'd still prefer to use log4net if possible, since the project I am testing uses it. Maybe my NLog example can give someone inspiration on how this could be done on log4net, and maybe they can help me figure it out :)
发布评论
评论(1)
您可能会对本文感兴趣:Log4Net:以编程方式指定多个记录器(具有多个文件附加程序)
另外,如果您只担心可读性,则可能有日志文件查看器可以按线程名称分隔日志条目。
您的另一种可能性是将条目记录在数据库中,包括您的线程名称,并且可以使用 sql 轻松过滤这些条目。
This article may be of interest to you: Log4Net: Programmatically specify multiple loggers (with multiple file appenders)
Also if you are only worried about readability there may be log file viewers that can seperate out log entries by thread name.
Another possibility you have is to log the entries in a database including your thread name and these entries are easily filtered using sql.