0

I am trying to debug the code for uploading the .xlsx file through my application in my local system. At the time of appending log data to the log file this exception

The process cannot access the file'C:\\Webconfigfiles\\Upload\\Save\\Log_20230706050233.txt' because it is being used by another process.

My doubt is that some of the data is already appended to the log file. (I have added a screenshot of the log file)

However, the rest of the log methods are throwing errors when appending data to my log file.

I cleared the application pool in my local IIS server, but it's still throwing the same error message. I have implemented dispose method by using statement at the time of creating appending log file. I have placed my code below

public Logger(string savePath)
{
    filePath = savePath + @"Log_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Create)))
    {
        writer.WriteLine("                                                             ");
        writer.WriteLine("-------------------------------------------------------------");
        writer.WriteLine(DateTime.Now.ToString());
        writer.WriteLine("-------------------------------------------------------------");
        writer.Close();
        //logFileStream.Close();
    }

}
public void WriteToLogFile(string Table, string Row, string refTable)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        //logFileStream = new FileStream(filePath, FileMode.Append);
        //logStreamWriter = new StreamWriter(logFileStream);
        writer.WriteLine("{0} table - Column value for {2} in row {1} of {0} table does not exist in {2} table", Table, Row, refTable);
        writer.Close();
        writer.Close();
    }
}
public void WriteToLogFileWithAppError(string Table, string Row, string Error)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("[{0} :] {1} table - {1} in row {2} with Error Message: {3}", DateTime.Now, Table, Row, Error);
        writer.Close();
        writer.Close();
    }

}
public void WriteToLogFileWithGeneralError(string Error)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("");
        writer.WriteLine("[{0} :] Error Message: {1}", DateTime.Now, Error);
        writer.Close();
        writer.Close();
    }
}
public void WriteToLogFile(string msg)
{
    using (var writer = new StreamWriter(new FileStream(filePath, FileMode.Append)))
    {
        writer.WriteLine("");
        writer.WriteLine("{0}", msg);
        writer.Close();
        writer.Close();
    }
}
Borisonekenobi
  • 469
  • 4
  • 15
  • 2
    Why do you call writer.Close() repeatedly? Please also post the consuming code e.g. the one calling the methods above. – Sancho Panza Jul 06 '23 at 12:37
  • 2
    Your Server is presumably called concurrently. So different request might need to log at the same time. So while one request is still inside the using block when another one wants to call the same code it will fail. YOu need to prevent concurrentl logging if you do logging this way. – Ralf Jul 06 '23 at 12:40
  • Further to Sancho's comment. if you wrap your code in a using, the stream automatically closes when it goes out of scope so you don't need the close statements. – The Betpet Jul 06 '23 at 12:41
  • Use a lock object to prevent the functions being called concurrently – Charlieface Jul 06 '23 at 14:18
  • You really should be using a logging library like [Serilog](https://serilog.net/). – Etienne de Martel Jul 07 '23 at 03:04

1 Answers1

0

You close the StreamWriter, but you must close the stream as well. Revise your code like this:

 using (var stream new FileStream(filePath, FileMode.Append)) {
    using (var writer = new StreamWriter(stream)
    {
        writer. WriteLine("");
        writer.WriteLine("{0}", msg);
    }
 }
Son of Man
  • 1,213
  • 2
  • 7
  • 27
Nick
  • 4,787
  • 2
  • 18
  • 24
  • 2
    Closing the inner stream is the default behavior of a StreamWriter. You need to prevent it via the proper constructor call (the leaveOpen parameter). In this code the stream is dispose twice now. – Ralf Jul 06 '23 at 12:41