Multipurpose of Exception Handling Class Part 2
July 5, 2007
Okay in this section part i will talk more detail about those kind of class ,
- Logging Class
Prerequisite for this class :
- You need provide the xml file for become the email template, the simple form like this :<EMAIL>
<TITLE>Error Notification</TITLE>
<MESSAGE>
<SUBJECT>Global Coding System Notification
</SUBJECT>
<BODY>ERROR MESSAGE ALERT {0}
================================================\n\nPlease be advised that somthing error happen in your application :
{1} \N
</BODY>
</MESSAGE>
</EMAIL>
- Those XML email file using two parameters, {0} this parameter will display the date error generate and the second things {1} this parameter will display or capture your exeception handling stack trace.
- Those parameter will be taken from the emailGenerator class, wheter triggered from logging class.
- In Logging class, there are two important method , first method is Handledexception Method hich has a duty to coordinate to another private method which has a function for logging writing to text file and also the trigerring to email alertGenerator class, and this class will become the second important method in this class.
So Let’s take a look the concrete code in this sample below :
//***********************************************************************************************************
// SUBJECT : LOGGING UTILITY ENGINE
// CREATED BY : Doddy Christiana Saputra , MCAD.NET
// LAST MODIFIED DATE : JUNE 22,2007
//***********************************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using FrameworkLayer.ConfigurationUtility;
using FrameworkLayer.EmailUtility;
using System.IO;
namespace FrameworkLayer.LoggingUtility
{
public class Logging
{
private bool boolHandleErrors;
private string strLastError;
private bool boolLogError;
private string strLogFile;
EmailGenerator Email = new EmailGenerator(); //Create instance of email generator
public bool HandleErrors
{
get
{
return boolHandleErrors;
}
set
{
boolHandleErrors = value;
}
}
public string LastError
{
get
{
return strLastError;
}
}
public bool LogErrors
{
get
{
return boolLogError;
}
set
{
boolLogError = value;
}
}
public string LogFile
{
get
{
return strLogFile;
}
set
{
strLogFile = value;
}
}
public void HandleExceptions(Exception ex)
{
this.LogErrors = true;
this.LogFile = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.ErrorLogFile;
if (LogErrors)
{
WriteToLog(ex.Message+ex.StackTrace);
}
if (HandleErrors)
{
strLastError = ex.Message;
}
else
{
throw ex;
}
}
private void WriteToLog(string msg)
{
StreamWriter writer = File.AppendText(LogFile);
writer.WriteLine(DateTime.Now.ToString() + ” – ” + msg);
writer.Close();
//This is the part of automatically sending email to programmer
string[] test = new string[1]; //harus didefine nanti
Email.AutomaticSendEmailLogging (msg); //hanyautk inisialisasi sementara
}
}
}
okay..after yo see all of those description then you will rather clear enough or getting more itchi
…
i will continue the emailgenerator class in the third part. see you..
Entry Filed under: Design Pattern Code, Development in C# 2.0, Own design custom Framework. .
Trackback this post | Subscribe to the comments via RSS Feed