-1

I have the C# console application which writes the step it is doing in a Log text file using the code below

logFilePath = logFilePath + "Log-" + System.DateTime.Today.ToString("dd-MM-yyyy") + "." + "txt";
logFileInfo = new FileInfo(logFilePath);
logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();
if (!logFileInfo.Exists)
{
    fileStream = logFileInfo.Create();
}
else
{
    fileStream = new FileStream(logFilePath,FileMode.Append);
}
log = new StreamWriter(fileStream);
log.WriteLine("---------------" + DateTime.Now.ToString() + "--------------------------");
log.WriteLine(strLog);
log.WriteLine("---------------------------------------------------------------");
log.Close();

It works fine but when an exception occurs I will attach the log text file to the mail and send it after that when I try to invoke the Method again which writes into the same log text file after mail with log text file attachment I get the exception IOException: The process cannot access the file 'file path' because it is being used by another process

I had tried FileShare Option and other things nothing worked out

Exception is thrown in the below code

client.Send(mail);

client.Dispose();

LogWriter.WriteLog("######--Re-Start of the Process at -" + Convert.ToString(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")) + "--######"); //Exception thrown here
Console.WriteLine("######--Re-Start of the Process at -" + Convert.ToString(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")) + "--######");
BridgePage.GetData();
Console.WriteLine("######--Re-End of the Process at -" + Convert.ToString(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")) + "--######");
LogWriter.WriteLog("######--Re-End of the Process at -" + Convert.ToString(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")) + "--######");

As per the comment to use FileInfo.AppendText by Panagiotis Kanavos tried the below code, but still same exception

logFilePath = logFilePath + "Log-" + System.DateTime.Today.ToString("dd-MM-yyyy") + "." + "txt";
logFileInfo = new FileInfo(logFilePath);

if (!logFileInfo.Exists)
{
   //Create a file to write to.
   using (StreamWriter logsw = logFileInfo.CreateText())
   {
       logsw.WriteLine("---------------" + DateTime.Now.ToString() + "--------------------------");
       logsw.WriteLine(strLog);
       logsw.WriteLine("---------------------------------------------------------------");
    }
 }
 else
 {
   using (StreamWriter logsw = logFileInfo.AppendText())
    {
       logsw.WriteLine("---------------" + DateTime.Now.ToString() + "--------------------------");
       logsw.WriteLine(strLog);
       logsw.WriteLine("---------------------------------------------------------------");
    }
  }

Please help

Rajesh
  • 1,600
  • 5
  • 33
  • 59
  • 3
    Try disposing of the `fileStream` as well. – Lasse V. Karlsen Mar 23 '21 at 08:52
  • 2
    Does this answer your question? [IOException: The process cannot access the file 'file path' because it is being used by another process](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – Sinatr Mar 23 '21 at 09:00
  • @Sinatr I had tried the FileShare mentioned in that question but it did not worked out, the exception is not thrown while accessing the file to append some line – Rajesh Mar 23 '21 at 09:12
  • 1
    Use a logging library instead of trying to build your own. Such problems are already solved by libraries like Serilog and NLog. Your code doesn't dispose the stream and writer objects the way it should, inside a `using` block, so any error will leave the file open. There's no need for the existence check either, [FileInfo.AppendText](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.appendtext?view=net-5.0) will create the file if it doesn't exist *and* open a StreamWriter. `DirectoryInfo.Create` will do nothing if the folder already exists – Panagiotis Kanavos Mar 23 '21 at 09:14
  • 1
    In fact, you could replace almost all of this code with [File.AppendAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=net-5.0) or [AppendAllLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalllines?view=net-5.0) – Panagiotis Kanavos Mar 23 '21 at 09:21

1 Answers1

0

Instead of Disposing the SmtpClient I had free'd the Attachments of MailMessage.Attachments after sending the mail

if (mail.Attachments != null)
{
    for (int i = mail.Attachments.Count - 1; i >= 0; i--)
    {
       mail.Attachments[i].Dispose();
    }
     mail.Attachments.Clear();
     mail.Attachments.Dispose();
 }
 mail.Dispose();
 mail = null;

It worked out

Rajesh
  • 1,600
  • 5
  • 33
  • 59