Click here to Skip to main content
15,894,405 members
Articles / Web Development / ASP.NET

Designing and implementing a versatile data access tier for an ASP.NET application

Rate me:
Please Sign up or sign in to vote.
4.63/5 (45 votes)
3 Feb 200328 min read 384.9K   3.8K   242  
In this article, we will drill down deeper in to the design of a n-tier architecture and our focus will be on the data access tier (DAT)
using System;
using System.Web;
using System.Diagnostics;
using System.Configuration;
using System.Text;



namespace HttpTraceModule
{
	/// <summary>
	/// It used to record errors on the presentation layer(PL)
	/// </summary>
	public class HttpTraceModule:IHttpModule
	{
		 
		/// <summary>
		/// Event Log for the Presentation Layer
		/// </summary>
		 EventLog elPL;
		/// <summary>
		/// 
		/// </summary>
		public HttpTraceModule()
		{
			// Initialize  Trace Listener
			string strLog = ConfigurationSettings.AppSettings["plLog"];
			string strSource = ConfigurationSettings.AppSettings["plSource"];
			string strMachine = ConfigurationSettings.AppSettings["plMachine"];
			if(EventLog.SourceExists(strLog,strMachine))
			{
				elPL = new EventLog(strLog,strMachine,strSource);
			}

		}

		/// <summary>
		/// interface contract method
		/// </summary>
		/// <param name="httpApplication"></param>
		public void Init(HttpApplication httpApplication)
		{
			// Subscribe HttpApplication events
           httpApplication.BeginRequest += new EventHandler(Application_BeginRequest);
		   httpApplication.EndRequest += new EventHandler(Application_EndRequest);
		}
		/// <summary>
		/// interface contract method
		/// </summary>
		public void Dispose()
		{
          
  
		}
		/// <summary>
		/// Writes errors on the log of the Presentation Layer 
		/// </summary>
		/// <param name="oException"></param>
		/// <param name="oContext"> </param>
		private void WriteError(HttpContext oContext,Exception oException)
		{
			StringBuilder oBuilder = new StringBuilder();
			// Record the requested URL and the client
			oBuilder.Append("An Error took place, while attempting to request  the  URL: ");
			oBuilder.Append(oContext.Request.RawUrl);
			oBuilder.Append("**");
			oBuilder.Append("Client Address:");
			oBuilder.Append(oContext.Request.UserHostAddress);
            oBuilder.Append("**");
			//  Record the error message
			if( oException != null)
			{
				oBuilder.Append( oException.Message);
			}
			if(elPL != null)
			{
				elPL.WriteEntry(oBuilder.ToString(),EventLogEntryType.Error);
			}

		}


		/// <summary>
		/// Callback method  for the event HttpApplication.EndRequest 
		/// It is used to record all errors on the log of the Presentation Layer 
		/// </summary>
		/// <param name="source"></param>
		/// <param name="e"></param>
		private void Application_EndRequest(object source, EventArgs e)
		{
			HttpApplication hApplication = (HttpApplication) source;
			HttpContext oContext = hApplication.Context;
			Exception oException = oContext.Error;
			if(oException != null)
			{
				this.WriteError(oContext,oException);
			}
		
		}

		/// <summary>
		/// Callback method  for the event HttpApplication.BeginRequest
		/// </summary>
		/// <param name="source"></param>
		/// <param name="e"></param>
		private void Application_BeginRequest(object source, EventArgs e)
		{
			HttpApplication hApplication = (HttpApplication) source;
			HttpContext oContext = hApplication.Context;
			
		}


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
Paul Abraham is a software developer who designs and develops multi-shop systems.

He has received his M.Sc in Mathematics and Computer Science from the FernUniversität Hagen(http://www.fernuni-hagen.de Germany) and his main interests are neural networks and bayesian statistics He lives in Rosenheim (South Germany http://www.rosenheim.de). You can reach him at admin@paul-abraham.com.

Comments and Discussions