Click here to Skip to main content
15,886,091 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.6K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Core
//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Globalization;
using System.Text;

namespace Transformalize.Libs.EnterpriseLibrary.Common.Instrumentation
{
    /// <summary>
    /// Formats an event log entry to the defined format.
    /// </summary>
    public class EventLogEntryFormatter : IEventLogEntryFormatter
    {
        private string applicationName;
        private string blockName;

        private static readonly string[] emptyExtraInformation = new string[0];

        /// <overloads>
        /// Initializes this object with the specified information.
        /// </overloads>
        /// <summary>
        /// Initializes this object with the name of the specific block using this class.
        /// </summary>
        /// <param name="blockName">Name of block using this functionality.</param>
        public EventLogEntryFormatter(string blockName)
            : this(GetApplicationName(), blockName)
        {
        }

        /// <summary>
        /// Initializes this object	with the given application and block names.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="blockName">Name of the block using this functionality.</param>
        public EventLogEntryFormatter(string applicationName, string blockName)
        {
            this.applicationName = applicationName;
            this.blockName = blockName;
        }

        /// <overloads>
        /// Creates a formatted message, suitable for logging to the event log.
        /// </overloads>
        /// <summary>
        /// Creates a formatted message, suitable for logging to the event log.
        /// </summary>
        /// <param name="message">Message to be formatted, with format tags embedded.</param>
        /// <param name="extraInformation">Extra strings to be matched up with the format tags provided in <paramref name="message"></paramref>.</param>
        /// <returns>Formatted message, suitable for logging to the event log.</returns>
        public string GetEntryText(string message, params string[] extraInformation)
        {
            return BuildEntryText(message, null, extraInformation);
        }

        /// <summary>
        /// Creates a formatted message, suitable for logging to the event log.
        /// </summary>
        /// <param name="message">Message to be formatted, with format tags embedded.</param>
        /// <param name="exception">Exception containing message text to be added to event log message produced by this method</param>
        /// <param name="extraInformation">Extra strings to be matched up with the format tags provided in <paramref name="message"></paramref>.</param>
        /// <returns>Formatted message, suitable for logging to the event log.</returns>
        public string GetEntryText(string message, Exception exception, params string[] extraInformation)
        {
            return BuildEntryText(message, exception, extraInformation);
        }

        private string BuildEntryText(string message, Exception exception, string[] extraInformation)
        {
            // add header
            StringBuilder entryTextBuilder
                = new StringBuilder(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.EventLogEntryHeaderTemplate,
                        applicationName,
                        blockName));
            entryTextBuilder.AppendLine();

            // add message
            entryTextBuilder.AppendLine(message);

            //add extra info
            for (int i = 0; i < extraInformation.Length; i++)
            {
                entryTextBuilder.AppendLine(extraInformation[i]);
            }

            // add exception
            if (exception != null)
            {
                entryTextBuilder.AppendLine(
                   string.Format(
                       CultureInfo.CurrentCulture,
                       Resources.EventLogEntryExceptionTemplate,
                       exception.ToString()));
            }

            return entryTextBuilder.ToString();
        }

        private static string GetApplicationName()
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }

        private string EntryTemplate
        {
            get
            {
                return "";
            }
        }
    }
}

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 GNU General Public License (GPLv3)


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