Sep 22, 2010

Create a Log File

This code snippet demonstrates the use to create a log file to store either your error or informational thing like storing executed queries etc.

Here you need to pass the message when you call this function. You can also specify drive in which you want your log files to be stored or else by default it will store in your project's bin folder.


public void writeToLogFile(String s)
{
    String Filename;
    FileStream file;
    file = new FileStream("BackupLog" +DateTime.Today.ToString("yyyyMMdd") + ".txt", FileMode.Append );
    TextWriter tw = new StreamWriter(file);
    tw.WriteLine("Log:" + DateTime.Now + ":- " + s);
    tw.Close();
}