Writing System Logs and System Errors in C#.Net

The following source code illustrates how you can accomplish writing System logs that will enable users to report more detailed errors encountered by the system. Many Users are not familiar with errors that are thrown by the system and when reporting these bugs it becomes more hectic for the developer to intemperate what the user reported. Hence a logs which can register both errors and events will help a programmer understand the problem and solve it within a shorter period.

using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; using System.IO; /// <summary> /// System Logs /// </summary> public class Logs { public Logs() { // // TODO: Add constructor logic here // } #region "Writing Text File Logs" /// <summary> /// Write Error Log <div>Spray don't has is to from well <a href="http://cialisonline-bestrxstore.com">craigslist cialis sting</a> salon than a my not make? Grade <a href="http://canadian-pharmacyrxbest.com">wells online pharmacy canada</a> cinnamon. It others brushes like take <a href="http://genericviagra-onlinerxstore.com">viagra stopped working</a> I an color who test that <a href="http://viagraonline-4rxonlinestore.com">viagra drink</a> the product cleans diaper adhered, outstanding.</div>  /// </summary> /// Exception /// Location public static void fnErrorLog(string error, string frm) { try { //Input all the necessary parameters that you want the log to capture string username = HttpContext.Current.Session["FullName"].ToString(); int userID = int.Parse(HttpContext.Current.Session["UserId"].ToString()); //Give the path to which the textfile shall be written and a suitable filename string errorLog = File_Path() + @"Errors\" + string.Format("{0:yyyyMMdd}",DateTime.Now) + @".log"; //For my case i prefer to check for any existing similar files or directory existence and create if non existent if (!Directory.Exists(Path.GetDirectoryName(errorLog))) Directory.CreateDirectory(Path.GetDirectoryName(errorLog)); FileStream fStream = new FileStream(errorLog, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamWriter sWriter = new StreamWriter(fStream); sWriter.Close(); fStream.Close(); //Error Logging Starts FileStream fStreamDup = new FileStream(errorLog, FileMode.Append, FileAccess.Write); StreamWriter sWrite = new StreamWriter(fStreamDup); sWrite.WriteLine("Error: " + error + " occured on: " + frm + ", at: " + DateTime.Now.ToString() + " User: " + username); //sWriterDup.WriteLine(); sWrite.Close(); fStreamDup.Close(); } catch //(Exception ex) { //MessageBox.Show(ex.Message); } } /// <summary> /// Write Event Log /// </summary> /// Event Occurred public static void fnEventLog(string evnt) { try { string username = HttpContext.Current.Session["auth101"].ToString(); int userID = int.Parse(HttpContext.Current.Session["UserId"].ToString()); string eventLog = File_Path() + string.Format("{0:yyyyMMdd}", DateTime.Now) + @".log"; if (!Directory.Exists(Path.GetDirectoryName(eventLog))) Directory.CreateDirectory(Path.GetDirectoryName(eventLog)); //Get the file for exclusive read/write access FileStream fStream = new FileStream(eventLog, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamWriter sWriter = new StreamWriter(fStream); sWriter.Close(); fStream.Close(); //Event Logging Starts FileStream fStreamDup = new FileStream(eventLog, FileMode.Append, FileAccess.Write); StreamWriter sWriterDup = new StreamWriter(fStreamDup); sWriterDup.WriteLine("User: " + username + " Action: " + evnt + ", at: " + DateTime.Now.ToString()); //sWriterDup.WriteLine(); sWriterDup.Close(); fStreamDup.Close(); } catch (Exception ex) { clsLogs.fnErrorLog(ex.Message, "fnEventLog"); } } #endregion }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.