Write Error in file Using this C# code we can write errors to files.
Create class for write error message to file
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO;
/// Summary description for clsErrorDisplay
public class clsErrorDisplay { public void WriteToFile(string ErrStr,string Page) { string FilePath = System.Web.HttpContext.Current.Server.MapPath("~/log/error.txt");//Add your path here
if (File.Exists(FilePath)) { FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); // Write to the file using StreamWriter class m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); m_streamWriter.WriteLine("\n"+Page + "\n"); m_streamWriter.WriteLine(ErrStr + "\n"); m_streamWriter.WriteLine("--------------------------------- \n "); m_streamWriter.Flush(); m_streamWriter.Dispose(); fs.Close(); } } }
Call WriteToFile() from your form... object.WriteToFile(ex.ToString(), Page.ToString());
|
No responses found. Be the first to respond and make money from revenue sharing program.
|