0

I would like to get all the logs files of my iis server and then send the content to a database.

This is my code:

string[] filePaths = Directory.GetFiles(@"C:\inetpub\logs\LogFiles\W3SVC1", "*.log",
                                     SearchOption.AllDirectories);

        foreach (var file in filePaths)
        {
            var lines = File.ReadAllLines(file);//exception
            foreach (var line in lines)
            {
                client.Send(line);                    
            }

        }

The problem is that I am getting an exception when I try to read the files of the folder:

IOException was unhandled
The process cannot access the file 'C:\inetpub\logs\LogFiles\W3SVC1\u_ex140625.log'     because it is being used by another process.

I understand the reason, but I cannot see which process is locking the file except the iis server itself but I cannot stop it, so how can I access to these logs files?

user2443476
  • 1,935
  • 9
  • 37
  • 66
  • Try to make a copy before reading it, ReadAllLines may be acquiring lock for read/write, if you copy it before (which only uses read lock) to a temp file you will be able to read it (if IIS does not acquires read lock) – Gusman Jun 25 '14 at 12:47
  • I prefer to avoid that solution, because I will have a FileSystemWatcher that permanently monitor all the logs files, so I cannot always create a new file at each newly line added. Is there a way to access directly to these files? – user2443476 Jun 25 '14 at 12:49
  • Try to open manually your files, instead of using ReadAllLines use a StreamReader and open your file with File.OpenRead() – Gusman Jun 25 '14 at 12:51

0 Answers0