Search code examples
c#fileasp.net-coreioexceptionserilog

How do I download a Serilog log file from the current day from my asp.net core web application?


I have an ASP.NET Core 2.0 Application. I'm creating a Dev only page that allows me to download the Serilog log file(s).

The log files are on a day rolling interval. I cannot download the current day's file. An IOException is thrown stating the file is being used by another process.

It seems only the log file for the current day is blocked.

Is there a way to release whatever has control on the file, download it, and then re-attach it?

public FileContentResult DownloadLog(string name){
string path = Directory.GetCurrentDirectory() + $"\\Logs\{name}"
byte[] = System.IO.File.ReadAllBytes(path); //Exception thrown here
return File(...);
}

Solution

  • When you configure Serilog with WriteTo.File(), you need to pass shared: true to allow other processes to read it concurrently. I.e.:

    .WriteTo.File("logs.txt", shared: true)