Search code examples
c#loggingstreamwriterlogfile

How can I open a Program with a logfil multiple times? (C#)


I have this code:

class Program
{
    static void Main(string[] args)
    {
        using (StreamWriter w = File.AppendText(@"C:\temp\log.txt"))
        {
            Log("Test1", w);
            Console.ReadLine();
        }
    }
    public static void Log(string logMessage, TextWriter w)
    {
        w.WriteLine(DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToLongTimeString());
        w.WriteLine(logMessage);
        w.WriteLine("-----------------------------------------------------------------------");
    }
}

when I start this code twice at the same time, I get an exception because the file is already opened in the other instance.

Is there any alternative to "StreamWriter" which can write the same file from two instances?


Solution

  • Actually StramWriter needs to close the file handler after writing something to it. So by putting Using statement, you gave this task (Closing the file handler) to the Using (it's One of it benefits) so if you wait in Using block for any reason (you have added a Console.ReadLine()) the file Handler will open until the end of that block.

    if correct above codes:

    class Program
    {
         static void Main(string[] args)
         {
             using (StreamWriter w = File.AppendText(@"C:\temp\log.txt"))
             {
                 Log("Test1", w);
             }
             Console.ReadLine();
         }
    
         public static void Log(string logMessage, TextWriter w)
         {
             w.WriteLine(DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToLongTimeString());
             w.WriteLine(logMessage);
             w.WriteLine("-----------------------------------------------------------------------");
         }
     }
    

    there should be a small chance of concurrency. but it may occur and you can use try catch block and while statement to insure that your log, has been submitted.

    class Program
    {
         static void Main(string[] args)
         {
             bool WriteDone = false;
             while(!WriteDone)
             {
                 try 
                 {
                     using (StreamWriter w = File.AppendText(@"C:\temp\log.txt"))
                     {
                        Log("Test1", w);
                     }
                     WriteDone = true;
                  } 
                  catch 
                  { 
                       System.Threading.Thread.Sleep(1000); // Wait for 1s and try again
                  }
             }
             Console.ReadLine();
         }
    
         public static void Log(string logMessage, TextWriter w)
         {
             w.WriteLine(DateTime.Now.ToShortDateString() + " | " + DateTime.Now.ToLongTimeString());
             w.WriteLine(logMessage);
             w.WriteLine("-----------------------------------------------------------------------");
         }
     }