ClassLibrary Project: Error Handling
using System;
public class ErrorHandlingCls{
string _ModuleName,_PageURL,_EventName;
public string ModuleName {
get{
return _ModuleName;}
set{
_ModuleName = value;}
}
public string PageURL{
get{
return _PageURL;}
set{
_PageURL = value;}
}
public string EventName{
get{
return _EventName;}
set{
_EventName = value;}
}
/// <summary>/// This method is for preapare the error massage on base of Exception Object/// </summary>/// <param name="serviceException"></param>/// <returns></returns>public static string CreateErrorMessage(Exception serviceException){
StringBuilder messageBuilder = new StringBuilder();
try{
messageBuilder.Append("The Exception is:-"); messageBuilder.Append(
"Exception :: " + serviceException.ToString());
if (serviceException.InnerException != null){
messageBuilder.Append(
"InnerException :: " + serviceException.InnerException.ToString());}
return messageBuilder.ToString();}
catch{
messageBuilder.Append("Exception:: Unknown Exception.");
return messageBuilder.ToString();}
}
/// <summary>/// This method is for writting the Log file with message parameter/// </summary>/// <param name="message"></param>public static void LogFileWrite(ErrorHandlingCls objErr, string message){
FileStream fileStream = null;
StreamWriter streamWriter = null;
try{
string logFilePath = "c:\\LogError\\";logFilePath = logFilePath +
"ProgramLog" + "-" + DateTime.Today.ToString("yyyyMMdd") + "." + "txt";
if (logFilePath.Equals("")) return;#region Create the Log file directory if it does not exists
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo = new FileInfo(logFilePath);logDirInfo =
new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();#endregion Create the Log file directory if it does not exists
if (!logFileInfo.Exists){
fileStream = logFileInfo.Create();
}
else{
fileStream = new FileStream(logFilePath, FileMode.Append);}
streamWriter =
new StreamWriter(fileStream);streamWriter.WriteLine(message);
}
finally{
if (streamWriter != null) streamWriter.Close();
if (fileStream != null) fileStream.Close();}
}
/// <summary>/// This method is for writting the Log file with message parameter/// </summary>/// <param name="message"></param>public static void LogFileWrite(string message){
FileStream fileStream = null;
StreamWriter streamWriter = null;
try{
string logFilePath = "c:\\LogError\\"; logFilePath = logFilePath +
"ProgramLog" + "-" + DateTime.Today.ToString("yyyyMMdd") + "." + "txt";
if (logFilePath.Equals("")) return;#region Create the Log file directory if it does not exists
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo = new FileInfo(logFilePath);logDirInfo =
new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();#endregion Create the Log file directory if it does not exists
if (!logFileInfo.Exists){
fileStream = logFileInfo.Create();
}
else{
fileStream = new FileStream(logFilePath, FileMode.Append);}
streamWriter =
new StreamWriter(fileStream);streamWriter.WriteLine(message);
}
finally{
if (streamWriter != null) streamWriter.Close();
if (fileStream != null) fileStream.Close();}
}
}
}
Default.aspx.cs
using ErrorHandling;
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace ClientAopp{
public partial class _Default : System.Web.UI.Page{
ErrorHandlingCls objError=new ErrorHandlingCls();
private void Page_Load(object sender, System.EventArgs e){
SqlDataReader objDataReader = null;
SqlConnection myConnection = null;
try{
string sql = "server=192.168.0.1;uid=username;pwd=mypass;database=mydb";myConnection =
new SqlConnection(sql);
SqlCommand myLogin = new SqlCommand("MyStoredProcedure", myConnection);myLogin.CommandType =
CommandType.StoredProcedure;
SqlParameter myParm1 = myLogin.Parameters.Add("@Id", SqlDbType.Int);myParm1.Value = 1;
myConnection.Open();
objDataReader = myLogin.ExecuteReader(
CommandBehavior.CloseConnection);}
catch (SqlException sqlex) // Catch SQL error message{
objError.ModuleName = "ABC";objError.PageURL =
"Default.aspx";objError.EventName =
"PageLoad";
ErrorHandlingCls.LogFileWrite(objError,ErrorHandlingCls.CreateErrorMessage(sqlex));}
catch (Exception ex) // Catch other exception message{
ErrorHandlingCls.LogFileWrite(objError, ErrorHandlingCls.CreateErrorMessage(ex));}
finally //Close DB objects{
if (objDataReader != null) objDataReader.Close();
if (myConnection != null) myConnection.Close();}
}
}
}

No comments:
Post a Comment