Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am trying to log error into a Application folder(its windows app). What i am trying to do is first check whether a file exists , if not create a new file or if it does exists write the error to the file.
But the problem i am facing is that the path of file. I want the Logs to be in Log folder inside bin. But how can i access that. If i take Applcation.StartUp it goes inside the debug folder and since the process is being done the file writing fails with the error"File already in use".
So how can i handle this. My desired path in ...\bin\Log..

Thanks
Posted

Use an existing component like log4net or my article : Mini Drop-in Replacement for log4net[^]
 
Share this answer
 
Comments
Arjun Menon U.K 24-Mar-14 2:52am    
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine(message);
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path);
tw.WriteLine(message);
tw.Close();
}


Any problem in this??
Mehdi Gholam 24-Mar-14 2:58am    
Try the following instead :

File.AppendAllText(path, message);
Arjun Menon U.K 24-Mar-14 3:04am    
what if the file don't exists?
Mehdi Gholam 24-Mar-14 3:05am    
http://msdn.microsoft.com/en-us/library/ms143356%28v=vs.110%29.aspx
Arjun Menon U.K 24-Mar-14 3:07am    
Can you please tell me , why if the File.Create(path); followed by the File.Append / stream wirter write function throwing error , already in use?
Hi

Can you try this to access the log folder inside bin?


string execPath = AppDomain.CurrentDomain.BaseDirectory.ToString();
           DirectoryInfo diInfo  = Directory.GetParent(execPath);
           string logPath = Path.Combine(diInfo.Parent.FullName, "logs");




Regards
Dominic
 
Share this answer
 
v2

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