Im getting exception on my logger text file say its in use by another process and i cant figure out

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In my constructor on form1 im trying to write something to the logger file:

if (Options_DB.Get_begin_downloads() == true)<br/>
{<br/>
Logger.Write("Download session started" + Environment.NewLine);

I used a breakpoint on the IF and Options_DB.Get_begin_downloads() is true so its entering and try to make the Logger.Write line.
Then its going to the Logeer.cs the logger class and try to create the file open it ( The file is already exist ).


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void Write(<span style="color:Blue; string str)
{
<span style="color:Blue; if (mut.WaitOne() == <span style="color:Blue; false)
{
<span style="color:Blue; return;
}
<span style="color:Blue; else
{

<span style="color:Blue; using (StreamWriter sw = <span style="color:Blue; new StreamWriter(full_path_log_file_name, <span style="color:Blue; true))
{
sw.Write(DateTime.Now.ToShortDateString() + <span style="color:#A31515; "--" + DateTime.Now.ToShortTimeString() + <span style="color:#A31515; " ==> " + str);
sw.WriteLine();
sw.Close();
}
}
mut.ReleaseMutex();
}
[/code]
<br/>
mut is Mutex

Im using the breakpoint clicking F11 to get forword line by line when its getting to the line: using (StreamWriter sw = new StreamWriter(full_path_log_file_name, true))<br/>
Im getting the exception error.


This is the full code of my Logger.cs class:


<div style="color:Black;background-color:White; <pre>
<span style="color:Green; /*----------------------------------------------------------------
* Module Name : Logger
* Description : A logger
* Author : Danny
* Date : 10/02/2010
* Revision : 1.00
* --------------------------------------------------------------*/


<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using WindowsFormsApplication1;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Threading;
<span style="color:Green; /*
* Introduction :
*
* This module is a logger. any module can use this
* module to log its actions.
*
*
* */

<span style="color:Green; /*----------------------------------------
* P R I V A T E D E F I N I T I O N S
* ---------------------------------------*/


<span style="color:Blue; namespace DannyGeneral
{
<span style="color:Blue; class Logger
{
<span style="color:Green; /*----------------------------------------
* P R I V A T E C O N S T A N T S
* ---------------------------------------*/
<span style="color:Blue; static <span style="color:Blue; string log_file_name = <span style="color:#A31515; @"logger.txt";
<span style="color:Blue; static <span style="color:Blue; string full_path_log_file_name;
<span style="color:Blue; static <span style="color:Blue; string path_log;
<span style="color:Blue; static Mutex mut;
<span style="color:Green; /*----------------------------------------
* P R I V A T E V A R I A B L E S
* ---------------------------------------*/

<span style="color:Green; /*---------------------------------
* P U B L I C M E T H O D S
* -------------------------------*/

<span style="color:Green; /*----------------------------------------------------------
* Function : Logger
* Description : static Constructor
* Parameters : none
* Return : none
* --------------------------------------------------------*/
<span style="color:Blue; static Logger()
{
mut = <span style="color:Blue; new Mutex();
path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath)+ <span style="color:#A31515; @"log";
<span style="color:Blue; if (!Directory.Exists(path_log))
{
Directory.CreateDirectory(path_log);
}
full_path_log_file_name = path_log + log_file_name;
}

<span style="color:Green; /*----------------------------------------------------------
* Function : Write
* Description : writes a string to the log file
* This functions will add time and date and
* end of line chars to the string written to
* the file.
* Parameters : string to write to the file.
* Return : none
* --------------------------------------------------------*/
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void Write(<span style="color:Blue; string str)
{
<span style="color:Blue; if (mut.WaitOne() == <span style="color:Blue; false)
{
<span style="color:Blue; return;
}
<span style="color:Blue; else
{

<span style="color:Blue; using (StreamWriter sw = <span style="color:Blue; new StreamWriter(full_path_log_file_name, <span style="color:Blue; true))
{
sw.Write(DateTime.Now.ToShortDateString() + <span style="color:#A31515; "--" + DateTime.Now.ToShortTimeString() + <span style="color:#A31515; " ==> " + str);
sw.WriteLine();
sw.Close();
}
}
mut.ReleaseMutex();
}
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void exist()
{
<span style="color:Blue; if (!File.Exists(path_log + log_file_name))
{
StreamWriter sw = <span style="color:Blue; new StreamWriter(path_log + log_file_name);
sw.Write(DateTime.Now.ToShortDateString()+<span style="color:#A31515; "--"+DateTime.Now.ToShortTimeString()+<span style="color:#A31515; " ==> "+<span style="color:#A31515; "First Time The Log File Was Created"+Environment.NewLine);
sw.WriteLine();
sw.Close();
}
}
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void newEmptyLine()
{
StreamWriter sw = <span style="color:Blue; new StreamWriter(path_log + log_file_name,<span style="color:Blue; true);
sw.WriteLine();
sw.Close();
}

<span style="color:Green; /*---------------------------------
* P R I V A T E M E T H O D S
* -------------------------------*/

}



}

[/code]

And this is the exception full message im getting:
The process cannot access the file C:UsersChocoladeAppDataLocalWindowsFormsApplication1My Weather Stationloglogger.txt because it is being used by another process.

System.IO.IOException was unhandled<br/>
Message=The process cannot access the file C:UsersChocoladeAppDataLocalWindowsFormsApplication1My Weather Stationloglogger.txt because it is being used by another process.<br/>
Source=mscorlib<br/>
StackTrace:<br/>
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)<br/>
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy,
Boolean useLongPath)<br/>
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)<br/>
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)<br/>
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)<br/>
at System.IO.StreamWriter..ctor(String path, Boolean append)<br/>
at DannyGeneral.Logger.Write(String str) in D:C-SharpDownload FileDownloading-File-Project-Version-012Downloading FileLogger.cs:line 87<br/>
at WindowsFormsApplication1.Form1..ctor() in D:C-SharpDownload FileDownloading-File-Project-Version-012Downloading FileForm1.cs:line 352<br/>
at WindowsFormsApplication1.Program.Main() in D:C-SharpDownload FileDownloading-File-Project-Version-012Downloading FileProgram.cs:line 25<br/>
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)<br/>
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br/>
at System.Threading.ThreadHelper.ThreadStart()<br/>
InnerException:

Line 352 on form1 is the line im trying to make the Logger.Write as showed above here.


Cant figure out whats the problem. The only time im using the logger in the constructor before trying to write to it is with the Logger.exist();
But the logger.exist() in the exist() function i did sw.Close(); so no reason that the logger file will be in use opr open.

Thanks for the help.
<hr class="sig danieli

View the full article
 
Back
Top