Thursday 6 February 2014

Creating simple error log in Asp.net / C#

 public void ErrorLog(string sMessage)
    {
     
        string LogPath = ConfigurationManager.AppSettings["ErrorlagPath"].ToString();
        string filename = "ErrorLog_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt";
        string filepath = LogPath + filename;

        if (!Directory.Exists(LogPath))
            Directory.CreateDirectory(LogPath);
        string sFilePath = filepath;

        StreamWriter objSw = new StreamWriter(sFilePath, true);
        objSw.WriteLine("-------------------START-------------" + DateTime.Now + " -----------------");
        objSw.WriteLine(sMessage);
        objSw.WriteLine("-------------------END-------------" + DateTime.Now + " -----------------");
        objSw.Close();
    }

No comments:

Post a Comment