C#中快速打开和关闭System.IO.StreamWriter

发布于 2024-08-29 20:33:51 字数 421 浏览 5 评论 0原文

假设您有一个文件,您正在以编程方式将有关进程的信息记录到其中。有点像典型的调试 Console.WriteLine,但由于您正在测试的代码的性质,您没有可写入的控制台,因此您必须将其写入文件之类的地方。我当前的程序使用 System.IO.StreamWriter 来完成此任务。

我的问题是关于使用 StreamWriter 的方法。是否最好只打开一个 StreamWriter 实例,执行所有写入操作,并在整个过程完成后关闭它?或者打开一个新的 StreamWriter 实例向文件中写入一行,然后立即关闭它,并在每次需要写入内容时执行此操作,这是一个更好的主意吗?在后一种方法中,这可能会通过针对给定消息执行此操作的方法来实现,而不是使用过多的行来使主流程代码膨胀。但有一种方法来帮助实现并不一定意味着它是更好的选择。选择一种方法或另一种方法是否有显着的优势?或者它们在功能上是等效的,将选择权留给程序员?

Suppose you have a file that you are programmatically logging information into with regards to a process. Kinda like your typical debug Console.WriteLine, but due to the nature of the code you're testing, you don't have a console to write onto so you have to write it somewhere like a file. My current program uses System.IO.StreamWriter for this task.

My question is about the approach to using the StreamWriter. Is it better to open just one StreamWriter instance, do all of the writes, and close it when the entire process is done? Or is it a better idea to open a new StreamWriter instance to write a line into the file, then immediately close it, and do this for every time something needs to be written in? In the latter approach, this would probably be facilitated by a method that would do just that for a given message, rather than bloating the main process code with excessive amounts of lines. But having a method to aid in that implementation doesn't necessarily make it the better choice. Are there significant advantages to picking one approach or the other? Or are they functionally equivalent, leaving the choice on the shoulders of the programmer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻梦 2024-09-05 20:33:52

查看预滚动日志记录实现;它们可能会帮你省去很多麻烦。显然,保持流打开意味着如果崩溃,您可能会丢失一些最终数据,但可以通过更多缓冲 IO 来提高性能。某些实现还可能提供诸如来自假脱机程序/队列的异步日志记录之类的功能。

Look at pre-rolled logging implementations; they may save you lots of headaches. Obviously keeping the stream open means you might lose some of the end data if it crashes, but may gaing performance from buffering the IO more. Some implementations may also offer features such as asynchronous logging from a spooler/queue.

浅忆流年 2024-09-05 20:33:52

为每次写入重复打开/关闭新的 StreamWriter 将为 GC 生成大量资源,并且由于每次打开操作都会实际查找文件,因此会给应用程序带来开销。另一方面,保持单个流打开将锁定文件。所以这取决于。

您不希望您的日志记录机制成为性能瓶颈,因此请写入单个流。使其无缓冲或自动刷新以进行关键调试(但请注意,这也会影响性能)。

我将遵循 log4net 模型,创建静态日志流,然后写入该单例。无论如何,请查看 log4net,这样您就不用自己动手了。 http://logging.apache.org/log4net/index.html

Repeated opening/closing a new StreamWriter for each write will generate a lot of resources for the GC plus impose overhead on the app due to the actual lookup of the file for every open operation. On the other hand, holding a single stream open will hold a lock on the file. So it depends.

You don't want your logging mechanism to become a performance bottleneck, so write to a single stream. Make it unbuffered or AutoFlush for critical debugging (but be advised, that also has a performance impact).

I'd follow the model of log4net, create a static log stream, and write to that singleton. Look into log4net anyway, so you don't roll your own. http://logging.apache.org/log4net/index.html

你的他你的她 2024-09-05 20:33:52

我会缓冲/排队数据&一旦达到阈值,就写入文件当应用程序正常关闭/退出时刷新整个队列。

唯一的问题是,如果应用程序崩溃,您可能会丢失队列中的项目......

HTH。

I would buffer/queue the data & write out to the file once it reaches a threshold & flush the whole queue when the application closes/exits normally.

The only issue in this is in case of an application crash, you might loose the items in the queue...

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文