如何为log4net提供您自己的时间戳 [英] how to provide your own timestamp for log4net

查看:166
本文介绍了如何为log4net提供您自己的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提供一个使用队列系统和Timer的Logger,以减少主线程上的日志记录时间.但是,我想捕获创建日志消息的时间,而不是记录日志消息的时间.虽然我可以在消息中添加时间戳,但是我希望可以将时间戳提供给log4net,以便可以使用模式提供程序来格式化日志消息.

I'm trying to provide a Logger using a queue system and Timer to to reduce logging time on my main thread. However I would like to capture the time in which a log message was created instead of when the log message was written. While I could add the timestamp to the message, I would like the option to provide the timestamp to log4net so that I can use the pattern provider to format the written log message.

如果无法提供时间戳记值.在记录之前轻松格式化消息模式的方法非常有用.

If providing the timestamp value is not possible. A way to easily format the message pattern before logging would be great.

例如:

<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />

-> 2011-12-17 00:00:38,511 [TestRunnerThread]调试GlobalLogger 12:00:38.411400 -第1行

--> 2011-12-17 00:00:38,511 [TestRunnerThread] DEBUG GlobalLogger 12:00:38.411400 - Line 1

推荐答案

当您查看log4net实现的内部结构时,您会发现在由附加程序编写日志时而不是在构造LoggingEvent实例时创建了时间戳.在log4net的抽象Logger类中,创建了一个新的LoggingEvent实例,该实例包含设置为DateTime.Now的时间戳.您可以在附加程序中使用此时间戳.

When you take a look at the log4net implementation internals you will see that the timestamp is not created when writing logs by the appender but when constructing instances of LoggingEvent. In the abstract Logger class of log4net a new LoggingEvent instance is created that contains a timestamp set to DateTime.Now. You can use this timestamp in your appenders.

这篇关于如何为log4net提供您自己的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆