Hi All,
I have implemented log functionality in my application which keeps track of all the user activities from the moment the application starts and till user exits the application. In My Application I create an instance of StreamWriter when the application begins and closing Stream Writer when the application exits.
My questions is when my application is running , I want to be able to go to the log file (\\program files\\MyApplication\\log.txt") and check the log contents. When I try to pen the log file it throws an error "An error occured. The document could not be opened". How can I view the log file and see the contents when the application is running and writing to the log file?
===============================================================
StreamWriter SW; //global variable
private void StartLogger()
{
string fPath = "\\Program Files\\My App\\log.txt;
SW = File.CreateText(fPath );
//Write in to log file
LogEntry("logfile created");
}
private void LogEntry(string entry)
{
SW.WriteLine(entry + "\n");
SW.Flush();
}
Thanks,
Ram
Loading
Kirtan PatelPosted Aug 19, 2009, 8:37 PM
Use Write() Function Like Below One , if you want to Access the File Frequently you shoud close Stream Writer after Every Writing like below Function .
dont forget to mark "do you like answer" please it will give me some credits :)
public void WriteLog(string s)
{
string LogFilePath = Directory.GetParent(Application.ExecutablePath).ToString() + @"\Log.txt";
StreamWriter sw = new StreamWriter(LogFilePath, true);
sw.WriteLine(s);
sw.Close();
}