简体   繁体   English

记录在多个线程中运行的.NET应用程序的最佳实践

[英]Logging best practices for .NET application running in multiple threads

We have an application that logs to files currently using streamwriter. 我们有一个应用程序可以记录当前使用Streamwriter的文件。 As we are looking at scaling up the application, the logging block is coming in the way. 当我们正在考虑扩大应用程序时,日志记录块即将来临。 The application is multi-threaded and can perform with a thread-count which is in thousands. 该应用程序是多线程的,并且可以执行具有数千个线程数的线程。 The thread-count does not hit the CPU as most of the responsibilities is to handle TCP sockets on the fly. 线程计数不会影响CPU,因为大多数职责是实时处理TCP套接字。 But the logging is an issue as there is convergence here and performance of the individual threads is impacted for its TAT here. 但是日志记录是一个问题,因为此处存在收敛,并且此处的TAT会影响各个线程的性能。

I shall like to get input from all of you regarding what kind of architecture/design can be followed for the loggging. 我希望从大家那里获得有关日志记录可以遵循的体系结构/设计的意见。

I am thinking of using database as the logging sink. 我正在考虑使用数据库作为日志接收器。 But the writer will have to have a locking mechanism I feel. 但是我认为作者必须具有锁定机制。 I don't think, I should try creating large number of loggers in threads. 我不认为,我应该尝试在线程中创建大量记录器。 Another option I am thinking of doing a pooling on top of the logger. 我正在考虑在记录器上进行池化的另一种选择。

Should I also look at MSMQ kind of technologies to quickly move out the traffic? 我是否还应该考虑使用MSMQ技术来快速消除流量?

I would look at existing logging frameworks, such as log4net or Microsoft's Enterprise Library Logging Application Block . 我将研究现有的日志记录框架,例如log4net或Microsoft的Enterprise Library Logging Application Block (I've used log4net a bit, but not the Logging Application Block.) (我曾经使用过log4net,但没有使用Logging Application Block。)

I very much doubt that your situation is unique, after all - and if you go for a third-party solution instead of a home-grown one, you'll be able to benefit from the experience of other developers. 毕竟,我非常怀疑您的情况是否独特-如果您选择第三方解决方案而不是自行开发的解决方案,那么您能否从其他开发人员的经验中受益。

If you do roll your own and want a producer/consumer model, you may want to look at the concurrent collections in .NET 4. As mhenrixon mentions though, you'll almost certainly have to find a balance between performance and reliability. 如果滚你自己,并希望生产者/消费者模型,你可能想看看并发集合在.NET 4作为mhenrixon提到,虽然,你几乎肯定要找到性能和可靠性之间的平衡。 If this is just for debug logs then it probably doesn't matter too much if you lose a few if the application shuts down abruptly - but obviously it's a different matter if the logs contain audit records which have to be persisted. 如果这只是调试日志,那么如果应用程序突然关闭,如果丢失了一些日志,可能没什么大不了的;但是,如果日志包含必须保留的审计记录,显然这是另一回事。

You should also consider NLog , particularly NLog 2.0 (currently in Beta - since September). 您还应该考虑使用NLog ,尤其是NLog 2.0(自9月起,目前处于Beta版)。 Among other things, NLog 2.0 adds the ability to log asynchronously . 除了其他功能外,NLog 2.0还增加了异步登录功能。 In essence, you would configure the Target(s) (equivalent to log4net Appender) to which you would like to log (file, database, etc). 从本质上讲,您将配置要记录到的目标(相当于log4net Appender)(文件,数据库等)。 Then, also via configuration, you "wrap" each Target with an asynchronous target wrapper. 然后,同样通过配置,用异步目标包装器“包装”每个目标。 NLog also has an option in the config file to make all logging asynchronous. NLog在配置文件中还有一个选项可以使所有日志记录异步。

While we you are on the subject of logging, you might also consider using a logging abstraction like Common.Logging for .NET . 当我们处于日志记录主题时,您可能还考虑使用诸如Common.Logging for .NET的日志记录抽象。 This gives you the option to defer the final decision on your underlying framework (log4net, NLog, etc) and also gives you the option to write your own logger (which can easily be exposed via Common.Logging). 这使您可以选择推迟对基础框架(log4net,NLog等)的最终决定,还可以选择编写自己的记录器(可以很容易地通过Common.Logging公开)。

It sounds like you are after something that in log4net is called Appender Buffering thought that one is not fail safe if logging cannot be "lossy" I recommend using queues. 听起来您好像在追随log4net中的一种叫做Appender Buffering的想法,认为如果日志记录不能“有损”,那不是不安全的,我建议使用队列。 I believe that any of the logging frameworks could easily be extended with MSMQ logging. 我相信,使用MSMQ日志记录可以轻松扩展任何日志记录框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM