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

Architecture guide: ASP.NET, MVC3, Entity Framework, Code-First with existing database, and a few more..

Rate me:
Please Sign up or sign in to vote.
4.72/5 (27 votes)
11 Dec 2013CPOL12 min read 154K   14.2K   134  
Developing a simple MVC 3 application architecture using Entity Framework’s Code First technology.
using System;

namespace Mvc3Demo.Common
{
	/// <summary>
	/// This is the base class for loggin purposes. 
	/// Any class that require to log errors/debug/info, it must be derived from this.
	/// </summary>
	public abstract class LoggerBase
	{
		#region Member Variables

		/// <summary>
		/// Member variable to hold the <see cref="ILog"/> instance.
		/// </summary>
		private readonly log4net.ILog logger = null;

		#endregion

		#region Properties

		/// <summary>
		/// Abstract property which must be overridden by the derived classes.
		/// The logger prefix is used to create the logger instance.
		/// </summary>
		protected abstract System.Type LogPrefix
		{
			get;
		}

		#endregion

		#region Constructors

        private static bool isConfigured = false;
		/// <summary>
		/// Constructor of the class.
		/// </summary>
		public LoggerBase()
		{
			// initiate logging class           
            if (!isConfigured)
            {
                log4net.Config.XmlConfigurator.Configure();
                isConfigured = true;
            }
            logger = log4net.LogManager.GetLogger(this.LogPrefix);
		}

		#endregion

		#region Methods

		#region Protected Methods

		/// <summary>
		/// Information level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		protected void LogInfo(string message)
		{
            if (this.logger.IsInfoEnabled)
            {
                this.logger.Info(message);
            }
		}

		/// <summary>
		/// Information level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		protected void LogInfo(string message, Exception e)
		{
			if (this.logger.IsInfoEnabled)
			{
				this.logger.Info(message, e);
			}
		}

		/// <summary>
		/// Warning level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		protected void LogWarn(string message)
		{
			if (this.logger.IsWarnEnabled)
			{
				this.logger.Warn(message);
			}
		}

		/// <summary>
		/// Warning level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		protected void LogWarn(string message, Exception e)
		{
			if (this.logger.IsWarnEnabled)
			{
				this.logger.Warn(message, e);
			}
		}

		/// <summary>
		/// Error level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		protected void LogError(string message)
		{
			if (this.logger.IsErrorEnabled)
			{
				this.logger.Error(message);
			}
		}

		/// <summary>
		/// Error level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged.</param>
		/// <param name="e">The exception that needs to be logged.</param>
		protected void LogError(string message, Exception e)
		{
            if (this.logger.IsErrorEnabled)
            {
                this.logger.Error(message, e);
            }
		}

		/// <summary>
		/// Debug level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		protected void LogDebug(string message)
		{
			if (this.logger.IsDebugEnabled)
			{
				this.logger.Debug(message);
			}
		}

		/// <summary>
		/// Debug level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		/// <param name="e">The exception that needs to be logged</param>
		protected void LogDebug(string message, Exception e)
		{
			if (this.logger.IsDebugEnabled)
			{
				this.logger.Debug(message, e);
			}
		}

		/// <summary>
		/// Fatal level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		protected void LogFatal(string message)
		{
			if (this.logger.IsFatalEnabled)
			{
				this.logger.Fatal(message);
			}
		}

		/// <summary>
		/// Fatal level messages are logged to the logger.
		/// </summary>
		/// <param name="message">String that needs to be logged</param>
		/// <param name="e">The exception that needs to be logged</param>
		protected void LogFatal(string message, Exception e)
		{
			if (this.logger.IsFatalEnabled)
			{
				this.logger.Fatal(message, e);
			}
		}

		#endregion

		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Virtusa Pvt. Ltd.
Sri Lanka Sri Lanka
In-depth coverage of Microsoft .Net, Cloud and many other cutting-edge Technologies.

- The Mandelbrot set – someone has called it the thumb-print of God – is one of the most beautiful and remarkable discoveries in the entire history of mathematics. My profile picture is generated with that equation.

You may contact Nirosh for Consultations, Code Reviews and Architecture Guide Workshops via c_nir*o*sh@hotmail.com (Remove * to use)



View Nirosh L.W.C.'s profile on LinkedIn


Other Links

Comments and Discussions