In this article, we will record errors and exceptions in our website through a simple process. We do this, each time we encounter a program error, the user navigates to a separate page, and the error will be logged to a text file on the server, and whenever an error occurs, we will log each day in the form of logs.
First, I write a static method to log the error message to a text file, here is the error message logged to the server in the error folder, the code is as follows:
Using System.Globalization; <summary>///For outputting error information to TXT file///</summary>//<param name= "errormessage" > Error details </para m> public static void Writeerror (String errormessage) {try {string path = ' ~/error/' + DateTime.Today.ToString ("yyMMdd") + ". txt"; if (! File.exists (System.Web.HttpContext.Current.Server.MapPath (path))) {File.create (System.Web.HttpC Ontext. Current.Server.MapPath (Path)). Close (); } using (StreamWriter w = file.appendtext (System.Web.HttpContext.Current.Server.MapPath (path))) { W.writeline ("\r\nlog Entry:"); W.writeline ("{0}", DateTime.Now.ToString (CultureInfo.InvariantCulture)); W.writeline (errormessage); W.writeline ("________________________________________________________"); W.flush (); W.close (); }} catch (Exception Ex) {Writeerror (ex. Message); } }
Asp. NET logging of error logs