在多线程应用程序中记录文本文件 [英] text file logging in multi threaded application

查看:145
本文介绍了在多线程应用程序中记录文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将vb.net程序从标准模式更改为多线程模型.

它作为Windows服务运行,没有GUI,并且在用户请求时同时根据多个用户的需要执行许多复杂的报告和文件生成功能.
对我来说,要查看程序的运行状况,至关重要的是要有良好的日志记录功能,当打开跟踪"功能时,当前可以将日志记录到文本文件中.它的应用程序每次运行都会在日志中捕获几百行.

我在多线程下使用单个文本文件时遇到一些问题.
我认为,尽管其他线程之间的处理时间安排不同,但数据库将使查询和跟踪每个线程变得容易.我尝试使用MySQL数据库存储日志,但问题是日志记录将应用程序从标准运行的3秒减慢到了34秒.这对性能有很大的影响.

现在,我回到使用多个文本文件的想法,然后再将其上传到MySQL,然后再处理逻辑表示.

我使用以下方法阅读了有关人的信息
TextWriter tw = TextWriter.Synchronized(File.AppendText(filePath));

我在想这会减慢线程之间的杂耍速度吗?
目前,我倾向于基于为该线程分配的GUID为每个线程生成文本文件名,然后稍后将所有不同文件的内容上传到MySQL.

这背后的想法是,如果每个线程都在运行自己的StreamWriter,则不会与要写入文本文件的其他任何线程同时进行并发操作.

目的是使所有内容在整个过程中尽可能快地运行.
关于多线程环境中的日志记录速度,是否有人对此有任何意见或经验?

I’m working towards changing a vb.net program from standard to a multi threaded model.

It runs as a windows service and has no GUI and performs a number of complex reporting and file generation functions on demand for the multiple users simultaneously when requested by the user.
For me to see what the program is doing it’s essential to have good logging which currently logs to a text file when "trace" is turned on. It app captures a few hundred lines in the log for each run.

I see some issues with using a single text file under multi threading.
I thought a database would make it easy to query and follow each thread despite the processing chronology of other threads in-between. I tried MySQL database for storing logs, the problem was logging slowed the app from 3 seconds for a standard run to 34 seconds. That’s a big performance hit.

Now I’m back to the idea of using multiple text files but then uploading it to MySQL afterwards and then dealing with logical presentation later.

I read about people using the below method
TextWriter tw = TextWriter.Synchronized(File.AppendText(filePath));

I’m thinking would that slow it down juggling between threads?
At the moment I’m leaning towards the idea of generating a text file name for each thread based on the GUID assigned for that thread and then later uploading the contents of all the disparate files into MySQL afterwards.

The thinking behind this is that if each thread has its own StreamWriter running it wont be juggling concurrency with any of the other threads wanting to write to a text file.

The objective is keep everything running as fast as possible the whole way.
Does anyone have any input or had experience around this with regard to speed of logging in a multi threading environment?

推荐答案

我很感谢您可能已经有了一个代码库,但可以查看 log4net [ ^ ]

我在大量的mutil线程环境中非常成功地使用了此方法.

http://stackoverflow.com/Questions/50213/are-with-loging-with-log4net-in-a-multi-threaded-environment [ http://stackoverflow.com/questions/1519211/multithread-safe-logging [ ^ ]

如果您不熟悉它,建议您尝试一下.没有意义重塑轮子:)
I appreciate you''ve probably already got a codebase, but possibly check out log4net[^]

I use this this very successfully in a high volume, mutil-threaded environment.

http://stackoverflow.com/questions/50213/are-there-any-issues-with-using-log4net-in-a-multi-threaded-environment[^]

http://stackoverflow.com/questions/1519211/multithread-safe-logging[^]

If you''re not familiar with it, I suggest you give it a go. No point reinventing the wheel :)


如果您仍然要为每个线程保留一个流,为什么不使用MemoryStream?除非您期望应用程序崩溃,否则为什么要使用文本文件来支持它?
If you are going to hold a stream for each thread anyway, why not use a MemoryStream? Why back it with a text file unless you expect the app to crash out?


如果需要记录,最好使用记录而不是基于家庭的技术.

请参阅我对这些问题的回复:

这是制作自定义记录器的方法(用于记录文本,UI或其他内容):
将MsBuild OutPut即时发送到TextBox Windows应用程序中 [ ^ ]

这是登录到系统日志的方法,但结构如下:
如何在文件夹下创建事件日志 [ ^ ]
我在这里提供了完整的包装器代码.

—SA
If you need logging, you better use logging, not home-based techniques.

See my post in response to these Questions:

This is how to make a custom logger (to log to text, to UI or whatever):
MsBuild OutPut to the TextBox on the fly in Windows Application[^]

This is how to log to the System log, but with structure:
How to create event log under a folder[^]
I offered a whole complete wrapper code here.

—SA


这篇关于在多线程应用程序中记录文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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