记录到内存中,然后写入文件 [英] Log to memory and then write to file

查看:280
本文介绍了记录到内存中,然后写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的东西,我有这个循环,对于所有天数(7次)运行,然后另一个循环里面,对于文件中的所有记录运行。 (约10万),因此所有在其所有的约70万次,现在我想日志中的每个循环的每个处理,并记录到一个文件,说我们是第一个循环的第一次,并且首次第二循环中,我们每一次,什么是在一个文件中完成登录。但问题是,如果我每次登录,它会非常疼的表现,因为这么多的IO操作的,我在想什么就是有什么办法,我可以每一步记录到内存(内存流或任何东西),然后在外层循环结束时,记录所有的内存流数据保存到文件?

I want to know something, I have this loop that runs for all days (7 times) and then another loop inside it, that runs for all the records in the file. (about 100000), so all in all its about a 700000 times, now I want to log each processing in each loop, and log that to a file, say we are inside 1st loop for first time, and 2nd loop for first time, we log each time, what is done in a file. But the problem is that if I were to log each time, it would terribly hurt the performance, because of so many IO operations, what I was thinking is that is there any way, I could log each and every step to memory (memory stream or anything) and then at the end of outer loop, log all that memory stream data to file?

说,如果我有

for (int i=0; i<7; i++)
{
  for (int j=0; j<RecordsCount; j++)
  {
    SomeOperation();
    // I am logging to a file here right now
  }
}

它伤害了可怕的性能,如果我删除日志文件,我可能会提前很多完成它。所以我想这将是更好地登录到存储第一和写所有的日志从内存到文件。那么,有没有办法做到这一点?如果有很多,什么是最好的?

Its hurting the performance terribly, if I remove the logging file, I could finish it a lot earlier. So I was thinking it would be better to log to memory first and write all that log from memory to file. So, is there any way to do this? If there are many, what's the best?

PS:这里是一个记录器,我发现的 http://www.codeproject.com/Articles/23424/TracerX-Logger-and-Viewer-for-NET ,但我可以用任何其他自定义记录器或任何东西,如果需要其他

ps: here is a logger I found http://www.codeproject.com/Articles/23424/TracerX-Logger-and-Viewer-for-NET, but I can use any other custom logger or anything else if required.

编辑:如果我用内存流,然后写这一切文件,将那给我更好的性能,然后用 File.AppendAllLines 由Yorye和zmbq回答的建议,也当会,让我过来有什么杰里米评论任何性能提升?

EDIT : If I use memory stream, and then write it all to file, would that give me better performance then using File.AppendAllLines as suggested in answer by Yorye and zmbq, also if would that give me any performance gain over what Jeremy commented?

推荐答案

为什么不使用正确的日志框架,而不是写你自己的?

Why not use a proper logging framework instead of writing your own?

NLOG例如已缓冲内置和很容易配置: http://nlog-project.org/wiki/BufferingWrapper_target

NLog for instance has buffering built in and it is easy to configure: http://nlog-project.org/wiki/BufferingWrapper_target

我建议你集中精力编写代码,让价值,你的项目,而重用所有其他的东西,现有的解决方案。这可能会令你更有效率,提供更好的结果: - )

I suggest you focus on writing code that gives value to your project while reusing existing solutions for all the other stuff. That will probably make you more efficient and give better results :-)

这篇关于记录到内存中,然后写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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