Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C# 4.0

INCLUDE in classic ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (4 votes)
5 Jul 2012CPOL3 min read 21.4K   641   12  
Implementing and using an INCLUDE method in classic ADO.NET.
using System;
using System.Threading;
using System.Diagnostics;

using EncodeFirst.Core;
using EncodeFirst.Core.Managers;

namespace EncodeFirst.TestService.Core.Managers
{
    public sealed class LogManager : ILogManager
    {   //Here, encapsulate some Log Framework (NLog, Log4Net, ...) 
        static readonly LogManager _instance = new LogManager();

        /// <summary>
        /// Gets the instance of LogManager.
        /// </summary>
        /// <remarks></remarks>
        public static LogManager Instance
        {
            get 
            {
                return _instance;
            }
        }

        LogManager()
        { 
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <remarks></remarks>
        public void Write(string message)
        {
            Write(message, EventLogEntryType.Information);
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The ex.</param>
        /// <remarks></remarks>
        public void Write(string message, Exception ex)
        {
            Write(message, ex, EventLogEntryType.Error);
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="logType">Type of the log.</param>
        public void Write(string message, EventLogEntryType logType)
        {
            Write(message, null, logType);
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception</param>
        /// <param name="logType">Type of the log.</param>
        public void Write(string message, Exception ex, EventLogEntryType logType)
        {
            Thread thread = new Thread(delegate()
            {
                try
                {   
                    if (message == null) message = string.Empty;

                    message += MessageHelper.GetExceptionMessages(ex);

                    //Write the message here, use some technology or framework (NLog, Log4Net, ...)
                }
                catch (Exception exc)
                {   //if fail call the last option method to write the log
                    WriteOnLastOption(message, ex, exc);
                }

            });

            thread.Start();
        }

        /// <summary>
        /// Writes the on last option.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="originalEx">The original exception.</param>
        /// <param name="ex">The current exception.</param>
        public void WriteOnLastOption(string message, Exception originalEx, Exception ex)
        {
            Thread thread = new Thread(delegate()
            {
                //Last option to write the log, write on a file or EventLog
            });

            thread.Start();
        }
    }
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions