1

I know the answer must be out there somewhere, I applied suggestions both from many other questions and from MSDN itself but I'm probably overlooking something here.

This is my method, I use it to dump output to file. lock object declaration attached for clarity.

private static Object fileLock = new Object();
private static void WriteToFile(string msg, bool WriteLine)
{
    lock (fileLock)
    {
        msg = DateTime.Now.ToShortTimeString() + " - " + msg;

        FileInfo F = new FileInfo("dump.txt");
        using (StreamWriter writer = F.Exists ? F.AppendText() : F.CreateText()) //<--THIS LINE THROWS
        {
            if (WriteLine)
                writer.WriteLine(msg);
            else
                writer.Write(msg);
        }
    }
}

Question is: Why does the using line above throws an IOException complaining another process is using the file the 2nd time I call the method ?

I'm calling it like this around my code:

Console.WriteLine(something)
#if(DEBUG)
    Extensions.WriteToFile(something,true);
#endif

Again, I'm sure this is a trivial issue and someone else asked something like this getting the right answer, but I'm unable to dig it up.

UPDATE

Refactoring out the FileInfo object and switching to File.XXX methods made the code work fine. I still wonder what the issue was, anyway the issue looks like solved.

Alex
  • 23,004
  • 4
  • 39
  • 73
  • 3
    That doesn't compile, as you are accessing an instance member (`fileLock`) from a static method. What does the declaration of the lock identifier actally look like? – Guffa Nov 24 '11 at 15:10
  • Are you running 2 instances of your application? – user7116 Nov 24 '11 at 15:48
  • @Guffa fileLock is static indeed, that line wasn't copypasted like the rest of the code. edited to fix. – Alex Nov 24 '11 at 15:53
  • @sixlettervariables only one instance running, happens during debug – Alex Nov 24 '11 at 15:56
  • 1
    My next question would be why don't you just hold open the file during logging? And what other, if any, methods may access `dump.txt`? Are you looking at it from outside? – user7116 Nov 24 '11 at 15:57
  • @sixlettervariables turns out i *did* overlook something. why should i keep opening/closing the file ? ^^ gonna try shuffling some code around and see what happens, then get back to you. BTW, no one else is touching that file. – Alex Nov 24 '11 at 16:04
  • question updated since a bit of refactoring apparently solved the issue. many thanks to everyone. – Alex Nov 25 '11 at 08:25

1 Answers1

2

@Guffa: declaration has to be private static object fileLock = new object();

@alex: Your code works just fine on my machine although it's a bit too complicated for the task imo.

static void Write(string text, string file)
    {
        using (StreamWriter sw = File.AppendText(file))// Creates or opens and appends
        {
            sw.WriteLine(text);
        }
    }

Maybe some antivirus or indexer locks your dump file.

  • accepted since refactoring out the FileInfo object seems to have solved the issue (wondering why, but still). thanks. – Alex Nov 25 '11 at 08:27