简体   繁体   English

UWP:编写巨大的日志文件非常慢

[英]UWP : writing a huge log file is very slow

I try to write a log file and it's huge amount of data. 我尝试编写一个日志文件,其中包含大量数据。 I use AppendTextAsync every time I want to add data to create the structure I want and it's very slow. 每当我想添加数据来创建所需的结构时,我都会使用AppendTextAsync,这非常慢。 Is there any way to improve it? 有什么办法可以改善吗?

        await FileIO.AppendTextAsync(file, "All event log messages   \r\n");

AppendTextAsync will open the file, seek to the end, write to the file, close the file. AppendTextAsync将打开文件,查找到最后,写入文件,然后关闭文件。 This involves a lot of work for the OS, especially for writing a single line of text. 这需要为OS进行大量工作,尤其是编写一行文本时。

Open the file once, append all the text required, then close it. 打开文件一次,附加所有必需的文本,然后将其关闭。

As somebody else mentioned AppendTextAsync open and write and close the file every time so I changed it to this: 正如其他人提到的, AppendTextAsync打开,写入和关闭文件,因此我将其更改为:

            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
            using (var outputStream = stream.GetOutputStreamAt(0))
            {

                using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
                {


                    dataWriter.WriteString("All event log messages   \r\n");
                    await dataWriter.StoreAsync();
                    await outputStream.FlushAsync();

                }
            }
            stream.Dispose();

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

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