In my Blazor server app I want to show the content of a server side file in my razor page. Th file is 5 MB and has appr. 100000 lines. (My app and the file are on the same server, Win2019) I read the file on the server line by line with "File.ReadLines". Reading of the file takes over 20 seconds.I don't know if that is normal for such a file size or amount of lines.
I wanted to know whether I can shorten the reading process, or even if there is a better way to show the content of the file (read only) on my razor page. The file is a dynamic file, that means its content is changing.
My razor code
<table>
@if(TagService.Error_log_loaded == true)
{
@for (int f = 0; f < TagService.Error_log.Count; f++)
{
<tr>
<td class=".log_file"><pre class="preformatted">@TagService.Error_log[f]</pre></td>
</tr>
}
}
</table>
My Blazor code:
public async Task Read_Error_Log()
{
try
{
TagService.Error_log.Clear();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var lines = File.ReadLines(CommonClass.error_path, Encoding.GetEncoding("Windows-1254"));
var line_trimmed = "";
foreach (var line in lines)
{
TagService.Error_log.Add(line);
}
TagService.Error_log_loaded = true;
StateHasChanged();
}
catch (Exception e)
{
using (StreamWriter sw = File.AppendText(CommonClass.error_path))
{
sw.WriteLine("Read_Error_Log() Error " + e + " " + Convert.ToString(DateTime.Now));
}
}
}
Not sure how you are doing this but here's a demo which uses async line by line reading to load the data into a
List<string>and theVirtualizecomponent to control the display rows retrieved. The data file I ran it on contains 1,000,000 lines. It loads the display immediately with the first set of data, while the service gets the rest of the data in the background.FileServiceregistered as a scoped service.Registered Services:
And the demo page.
It will need a little tuning and sorting for your situation.
There's a demo Repo here : https://github.com/ShaunCurtis/SO76986556