1

I am logging my data by Serilog and I am not able to view the log files of the current day because they are used by another process.

I created logger in my program.cs and it is logging data very well

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(Configuration)
    .WriteTo.File(new JsonFormatter(), "Logs/Log.txt", 
            shared:true, rollingInterval: RollingInterval.Day)
    .CreateLogger();

I want to be able to view the logs of the current day

  • Have you navigated to the Logs path in the file explorer and attempted to open them with a text editor of your choice ? If you'r running your code and attempting to look at the log files in your IDE it could be that the IDE has a lock on the files when running. – Darren Sep 14 '19 at 13:29
  • How are you trying to open the file when you get the message saying it's being used by another process? [Serilog does **not** lock the log file](https://github.com/serilog/serilog-sinks-file/search?q=FileShare&unscoped_q=FileShare)... So you should be able to view its contents – C. Augusto Proiete Sep 14 '19 at 17:42
  • @CaioProiete I want to open it by FTP connection. But my app is blocking it. It is not possible to copy the file – Hermes Khachatrian Sep 16 '19 at 07:05
  • @Darren in the local machine it works with a text editor, but when the app in a production environment it is not possible to open it by editors – Hermes Khachatrian Sep 16 '19 at 07:06
  • @HermesKhachatrian Are you sure the it's the app that is blocking it? It could be that you don't have permission to read files in the FTP (i.e.not related to your app or to Serilog at all) – C. Augusto Proiete Sep 16 '19 at 21:27
  • @CaioProiete Yes I am sure. When I try to open other log files of the previous days which are no longer being written the FTP opens them. When I try to open the file of current day which is storing log data at current moment FTP does not download it. – Hermes Khachatrian Sep 18 '19 at 07:07
  • @HermesKhachatrian It seems that your FTP server is requiring exclusive lock to the file, before it allows you to copy it... Maybe you can change that in the settings of your FTP server. As you saw in my comment above, [Serilog is not preventing other processes from reading and writing to the file](https://github.com/serilog/serilog-sinks-file/search?q=FileShare&unscoped_q=FileShare) but, of course, any process that asks for an exclusive lock, will be denied because Serilog has an open handle to the file. – C. Augusto Proiete Sep 20 '19 at 00:33
  • @HermesKhachatrian On a different note, have you considered using a more modern way of visualizing logs from your apps, such as [Seq](https://datalust.co/seq)? Copying log files via FTP is the kind of things people did in 1995 ;) ;) – C. Augusto Proiete Sep 20 '19 at 00:35

1 Answers1

1

This is answer for getting contents of log file from code, not via FTP, but maybe it can be useful for someone struggling with getting contents from code (like I was).

My original solution for reading the log file was

var path = "...";
var fileBytes = await System.IO.File.ReadAllBytesAsync(path);

return File(fileBytes, "text/plain", fileName);

With this code I was still getting an error that the file is being used by another process. Then I looked into the method ReadAllBytesAsync and there I found this line

new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read,...)

and I found the parameters FileShare interesting. So I looked, what other possibilities I can put there and I found FileShare.ReadWrite, so I tried it and it worked. Like this

var path = "...";
byte[] result;
  
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
  result = new byte[stream.Length];
  await stream.ReadAsync(result, 0, (int)stream.Length);
}

return File(result, "text/plain", fileName);

When I tried the same (the last code snippet) with FileShare.Read, the error was showing again.

So Serilog has the log file opened for (reading?) and writing and when I want to open the file for reading I have to still allow other processes to write to it and therefore I have to use FileShare.ReadWrite.

Finally I searched if my assumptions were correct and I found this nice explanation for the FileShare parameter - https://stackoverflow.com/a/25098500/6277745