how to provide your own timestamp for log4net

2019-07-12 20:40发布

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.

For example:

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

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

2条回答
等我变得足够好
2楼-- · 2019-07-12 20:53

This might be a little late now, but there is code for a sample asynchronous appender here

查看更多
地球回转人心会变
3楼-- · 2019-07-12 21:04

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.

查看更多
登录 后发表回答