Exception handler levels
Code level
Use try/catch/finally block
Page level
Use Page_error
[js]
public void Page_Error(Object sender, EventArgs e)
{
// Implementation here
}
[/js]
Application level (global.asax)
Use Application_Error
[js]
public void Application_Error(Object sender, EventArgs e)
{
// Implementation here
}
[/js]
Log message types
Export ‘Exception’ message
[js]using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace DBClasses
{
public static class Error
{
public static void LogErrorToDB(Exception ex)
{
//// Error source detail
//StackTrace trace = new StackTrace(ex, true);
//StackFrame stackFrame = trace.GetFrame(trace.FrameCount – 1);
//string fileName = “file: ” + stackFrame.GetFileName();
//string methodName = “method: ” + stackFrame.GetMethod().Name;
//string lineNo = “line no.: ” + stackFrame.GetFileLineNumber().ToString();
//string errorSrc = fileName + “, ” + methodName + “, ” + lineNo;
// Exception message
StringBuilder sb = new StringBuilder();
sb.Append(Environment.NewLine);
sb.Append(“********************” + ” Error Log – ” + DateTime.Now + “*********************”);
sb.Append(Environment.NewLine);
sb.Append(“Exception Type : ” + ex.GetType().Name);
sb.Append(Environment.NewLine);
sb.Append(“Error Message : ” + ex.Message);
sb.Append(Environment.NewLine);
sb.Append(“Error Source : ” + ex.Source);
sb.Append(Environment.NewLine);
if (ex.StackTrace != null)
{
sb.Append(“Error Trace : ” + ex.StackTrace);
}
Exception innerEx = ex.InnerException;
while (innerEx != null)
{
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append(“Exception Type : ” + innerEx.GetType().Name);
sb.Append(Environment.NewLine);
sb.Append(“Error Message : ” + innerEx.Message);
sb.Append(Environment.NewLine);
sb.Append(“Error Source : ” + innerEx.Source);
sb.Append(Environment.NewLine);
if (ex.StackTrace != null)
{
sb.Append(“Error Trace : ” + innerEx.StackTrace);
}
innerEx = innerEx.InnerException;
}
// Write to file
string filePath = HttpContext.Current.Request.PhysicalApplicationPath + (“ErrorLog.txt”);
StreamWriter writer;
if (!File.Exists(filePath))
{
writer = new StreamWriter(filePath);
}
else
{
writer = File.AppendText(filePath);
}
// Write to the file:
writer.WriteLine(sb.ToString());
writer.Flush();
writer.Close();
// Write to OS log
//if (!EventLog.SourceExists(“MainApplication”))
// EventLog.CreateEventSource(“MainApplication”, “Application”);
//if (EventLog.SourceExists(“MainApplication”))
//{
// EventLog eventlog = new EventLog(“InfotrakLogs”);
// eventlog.Source = “MainApplication”;
// eventlog.WriteEntry(sb.ToString(), EventLogEntryType.Error);
//}
//// Write to DB
//DBase db = new DBase();
//SqlParameter[] prm = new SqlParameter[4];
//prm[0] = new SqlParameter(“@ExceptionMessage”, SqlDbType.NVarChar);
//prm[0].Value = sb.ToString();
//prm[1] = new SqlParameter(“@Source”, SqlDbType.VarChar);
//prm[1].Value = errorSrc;
//db.ExecuteNonQuery(“save_error_log”, prm);
}[/js]
Export free message
[js] public static void LogMessage(String msg)
{
// Exception message
StringBuilder sb = new StringBuilder();
sb.Append(Environment.NewLine);
sb.Append(“********************” + ” Error Log – ” + DateTime.Now + “*********************”);
sb.Append(Environment.NewLine);
sb.Append(msg);
// Write to file
string filePath = HttpContext.Current.Request.PhysicalApplicationPath + (“ErrorLog.txt”);
StreamWriter writer;
if (!File.Exists(filePath))
{
writer = new StreamWriter(filePath);
}
else
{
writer = File.AppendText(filePath);
}
// Write to the file:
writer.WriteLine(sb.ToString());
writer.Flush();
writer.Close();
}[/js]
Export DataTable content
[js] public static void WriteDataTable(DataTable dt)
{
string outputFilePath = HttpContext.Current.Request.PhysicalApplicationPath + (“ErrorLog.txt”);
int[] maxLengths = new int[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
maxLengths[i] = dt.Columns[i].ColumnName.Length;
foreach (DataRow row in dt.Rows)
{
if (!row.IsNull(i))
{
int length = row[i].ToString().Length;
if (length > maxLengths[i])
{
maxLengths[i] = length;
}
}
}
}
using (StreamWriter sw = new StreamWriter(outputFilePath, false))
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2));
}
sw.WriteLine();
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (!row.IsNull(i))
{
sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2));
}
else
{
sw.Write(new string(' ', maxLengths[i] + 2));
}
}
sw.WriteLine();
}
sw.Close();
}
}
}
}[/js]
Leave a Reply