Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"The process cannot access the file '2016-02-26-13.txt' because it is being used by another process."Error log not working."The process cannot access the file '2016-02-26-13.txt' because it is being used by another process."Error log not working

C#
public static void ErrLog(string filename, string methodname, string errorMsg)
{
  string path = AppDomain.CurrentDomain.BaseDirectory + @"ErrLogReport\";
  string dir = Path.GetDirectoryName(path);

  if (!Directory.Exists(dir))
  {
      Directory.CreateDirectory(dir);
  }

  string strFileName = string.Format(@"{0}\{1}.txt", path, DateTime.Now.ToString("ddMMMyyyy_HH"));
  StreamWriter sw = new StreamWriter(strFileName, true);

  sw.WriteLine(string.Format("{0}", DateTime.Now.ToString("[hh:mm:ss:fff]")));

  if (filename.Length > 0)
      sw.Write("File Name: " + filename);

  if (methodname.Length > 0)
    sw.WriteLine(" Method Name: " + methodname);

  sw.WriteLine("ErrorMsg: " + errorMsg);
  sw.WriteLine("---");

  sw.Flush();
  sw.Close();
}


What I have tried:

done coding.But i am using multi threading operation.It calls simtaneously in error log method.
Posted
Updated 28-Feb-16 22:08pm
v2
Comments
StM0n 29-Feb-16 1:31am    
As you already mentioned, you're writing simultaneously... lock the writing... are you able to use a logging framework?
SATHISH kumar 29-Feb-16 1:35am    
No,not using
StM0n 29-Feb-16 1:41am    
You're not able to use or you are not using one? If you're able to use one, do that; they're plenty to choose one :)
SATHISH kumar 29-Feb-16 1:36am    
when i lock writing ,other when writing or not writing
StM0n 29-Feb-16 1:40am    
Maybe you should look up "lock csharp" and give it a reading.

Multi threading is a difficult beast, don't write your own code unless you are very experienced, use one of these instead :
Mini Drop-in Replacement for log4net[^]
Apache log4net – Apache log4net: Home[^]
 
Share this answer
 
You must lock the code portions that access the file. See the lock Statement (C# Reference)[^].

Because your logging function is static, you must create a static lock object:
C#
static object lockErrLog = new object();

public static void ErrLog(string filename, string methodname, string errorMsg)
{
    // Code that does not require locking

    // Lock file access
    lock (lockErrLog)
    {
        StreamWriter sw = new StreamWriter(strFileName, true);
        // Write to stream here
        sw.Close();
    }
}
 
Share this answer
 
Comments
SATHISH kumar 29-Feb-16 4:15am    
when i writing using with Lock object,others when writing.
Jochen Arndt 29-Feb-16 4:18am    
???

From the above link:
"The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900