Sep 22, 2010

Create a Log File in ASP.Net

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();
}

2 comments:

  1. nice blog and very useful to people like me who want to refresh .net! and thanks for trying out my sambar :)

    ReplyDelete