Click here to Skip to main content
15,881,882 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.5K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;

namespace Transformalize.Libs.NLog.Config
{
    /// <summary>
    ///     Provides context for install/uninstall operations.
    /// </summary>
    public sealed class InstallationContext : IDisposable
    {
#if !SILVERLIGHT && !NET_CF
        /// <summary>
        ///     Mapping between log levels and console output colors.
        /// </summary>
        private static readonly Dictionary<LogLevel, ConsoleColor> logLevel2ConsoleColor = new Dictionary<LogLevel, ConsoleColor>
                                                                                               {
                                                                                                   {LogLevel.Trace, ConsoleColor.DarkGray},
                                                                                                   {LogLevel.Debug, ConsoleColor.Gray},
                                                                                                   {LogLevel.Info, ConsoleColor.White},
                                                                                                   {LogLevel.Warn, ConsoleColor.Yellow},
                                                                                                   {LogLevel.Error, ConsoleColor.Red},
                                                                                                   {LogLevel.Fatal, ConsoleColor.DarkRed},
                                                                                               };
#endif

        /// <summary>
        ///     Initializes a new instance of the <see cref="InstallationContext" /> class.
        /// </summary>
        public InstallationContext()
            : this(TextWriter.Null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="InstallationContext" /> class.
        /// </summary>
        /// <param name="logOutput">The log output.</param>
        public InstallationContext(TextWriter logOutput)
        {
            LogOutput = logOutput;
            Parameters = new Dictionary<string, string>();
            LogLevel = LogLevel.Info;
        }

        /// <summary>
        ///     Gets or sets the installation log level.
        /// </summary>
        public LogLevel LogLevel { get; set; }

        /// <summary>
        ///     Gets or sets a value indicating whether to ignore failures during installation.
        /// </summary>
        public bool IgnoreFailures { get; set; }

        /// <summary>
        ///     Gets the installation parameters.
        /// </summary>
        public IDictionary<string, string> Parameters { get; private set; }

        /// <summary>
        ///     Gets or sets the log output.
        /// </summary>
        public TextWriter LogOutput { get; set; }

        /// <summary>
        ///     Logs the specified trace message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="arguments">The arguments.</param>
        public void Trace([Localizable(false)] string message, params object[] arguments)
        {
            Log(LogLevel.Trace, message, arguments);
        }

        /// <summary>
        ///     Logs the specified debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="arguments">The arguments.</param>
        public void Debug([Localizable(false)] string message, params object[] arguments)
        {
            Log(LogLevel.Debug, message, arguments);
        }

        /// <summary>
        ///     Logs the specified informational message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="arguments">The arguments.</param>
        public void Info([Localizable(false)] string message, params object[] arguments)
        {
            Log(LogLevel.Info, message, arguments);
        }

        /// <summary>
        ///     Logs the specified warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="arguments">The arguments.</param>
        public void Warning([Localizable(false)] string message, params object[] arguments)
        {
            Log(LogLevel.Warn, message, arguments);
        }

        /// <summary>
        ///     Logs the specified error message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="arguments">The arguments.</param>
        public void Error([Localizable(false)] string message, params object[] arguments)
        {
            Log(LogLevel.Error, message, arguments);
        }

        /// <summary>
        ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (LogOutput != null)
            {
                LogOutput.Close();
                LogOutput = null;
            }
        }

        /// <summary>
        ///     Creates the log event which can be used to render layouts during installation/uninstallations.
        /// </summary>
        /// <returns>Log event info object.</returns>
        public LogEventInfo CreateLogEvent()
        {
            var eventInfo = LogEventInfo.CreateNullEvent();

            // set properties on the event
            foreach (var kvp in Parameters)
            {
                eventInfo.Properties.Add(kvp.Key, kvp.Value);
            }

            return eventInfo;
        }

        private void Log(LogLevel logLevel, [Localizable(false)] string message, object[] arguments)
        {
            if (logLevel >= LogLevel)
            {
                if (arguments != null && arguments.Length > 0)
                {
                    message = string.Format(CultureInfo.InvariantCulture, message, arguments);
                }

#if !SILVERLIGHT && !NET_CF
                var oldColor = Console.ForegroundColor;
                Console.ForegroundColor = logLevel2ConsoleColor[logLevel];

                try
                {
                    LogOutput.WriteLine(message);
                }
                finally
                {
                    Console.ForegroundColor = oldColor;
                }
#else
                this.LogOutput.WriteLine(message);
#endif
            }
        }
    }
}

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