Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.5K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Log4netExtensions.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Log4net extensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

#if SILVERLIGHT
using Catel.Diagnostics;
#else
using System.Diagnostics;
#endif

namespace log4net
{
	/// <summary>
	/// Log4net extensions.
	/// </summary>
	public static class log4netExtensions
	{
		#region Variables
        /// <summary>
        /// A dictionary containing all living instances of the stopwatches.
        /// </summary>
		private static readonly Dictionary<string, Stopwatch> _stopwatchTracers = new Dictionary<string, Stopwatch>();
		#endregion

		#region Methods
		#region Stopwatch
		/// <summary>
		/// Starts the trace of a stopwatch trace. Should be ended with a call to <see cref="StopStopwatchTrace"/>.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="name">The name of the stopwatch.</param>
		public static void StartStopwatchTrace(this ILog log, string name)
		{
			StartStopwatchTrace(log, name, true);
		}

		/// <summary>
		/// Starts the trace of a stopwatch trace. Should be ended with a call to <see cref="StopStopwatchTrace"/>.
		/// </summary>
        /// <param name="log">The log to write to.</param>
        /// <param name="name">The name of the stopwatch.</param>
		/// <param name="traceNameOnStart">if set to <c>true</c>, the name will be traced both on the start and stop of the stopwatch.</param>
		public static void StartStopwatchTrace(this ILog log, string name, bool traceNameOnStart)
		{
			if (_stopwatchTracers.ContainsKey(name))
			{
				_stopwatchTracers[name].Stop();
				_stopwatchTracers[name].Reset();
				_stopwatchTracers[name].Start();
			}
			else
			{
				_stopwatchTracers.Add(name, new Stopwatch());
				_stopwatchTracers[name].Start();
			}

			if (traceNameOnStart)
			{
				log.Debug(name);
			}
		}

		/// <summary>
		/// Stops the stopwatch trace and writes the duration as Verbose to the trace.
		/// </summary>
        /// <param name="log">The log to write to.</param>
        /// <param name="name">The name of the stopwatch.</param>
		public static void StopStopwatchTrace(this ILog log, string name)
		{
			if (!_stopwatchTracers.ContainsKey(name))
			{
				return;
			}

			_stopwatchTracers[name].Stop();

			log.Debug("{0} took {1}", name, _stopwatchTracers[name].Elapsed);

			_stopwatchTracers.Remove(name);
		}
		#endregion

		#region Debug
		/// <summary>
		/// Writes a formatted message to the log as debug.
		/// </summary>
        /// <param name="log">The log to write to.</param>
		/// <param name="format">The formatted text.</param>
		/// <param name="args">The arguments for the format.</param>
		public static void Debug(this ILog log, string format, params object[] args)
		{
			log.DebugFormat(format, args);
		}

		/// <summary>
		/// Writes a message to the log as debug with exception details.
		/// </summary>
        /// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void Debug(this ILog log, Exception ex, string message)
		{
			log.Debug(message, ex);
		}

		/// <summary>
		/// Writes a formatted message to the log as debug with exception details.
		/// </summary>
        /// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Debug(this ILog log, Exception ex, string format, params object[] args)
		{
			log.Debug(string.Format(format, args), ex);
		}
		#endregion

		#region Info
		/// <summary>
		/// Writes a formatted message to the log as information.
		/// </summary>
        /// <param name="log">The log to write to.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Info(this ILog log, string format, params object[] args)
		{
			log.InfoFormat(format, args);
		}

		/// <summary>
		/// Writes a message to the log as info with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void Info(this ILog log, Exception ex, string message)
		{
			log.Info(message, ex);
		}

		/// <summary>
		/// Writes a formatted message to the log as info with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Info(this ILog log, Exception ex, string format, params object[] args)
		{
			log.Info(string.Format(format, args), ex);
		}
		#endregion

		#region Warn
		/// <summary>
		/// Writes a formatted message to the log as warning.
		/// </summary>
		/// <param name="log">The log to write to.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Warn(this ILog log, string format, params object[] args)
		{
			log.WarnFormat(format, args);
		}

		/// <summary>
		/// Writes a message to the log as warning with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void Warn(this ILog log, Exception ex, string message)
		{
			log.Warn(message, ex);
		}

		/// <summary>
		/// Writes a formatted message to the log as warning with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Warn(this ILog log, Exception ex, string format, params object[] args)
		{
			log.Warn(string.Format(format, args), ex);
		}
		#endregion

		#region Error
		/// <summary>
		/// Writes a formatted message to the log as error.
		/// </summary>
		/// <param name="log">The log to write to.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Error(this ILog log, string format, params object[] args)
		{
			log.ErrorFormat(format, args);
		}

		/// <summary>
		/// Writes a message to the log as error with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void Error(this ILog log, Exception ex, string message)
		{
			log.Error(message, ex);
		}

		/// <summary>
		/// Writes a formatted message to the log as error with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Error(this ILog log, Exception ex, string format, params object[] args)
		{
			log.Error(string.Format(format, args), ex);
		}
		#endregion

		#region Fatal
		/// <summary>
		/// Writes a formatted message to the log as fatal.
		/// </summary>
		/// <param name="log">The log to write to.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Fatal(this ILog log, string format, params object[] args)
		{
			log.FatalFormat(format, args);
		}

		/// <summary>
		/// Writes a message to the log as fatal with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void Fatal(this ILog log, Exception ex, string message)
		{
			log.Fatal(message, ex);
		}

		/// <summary>
		/// Writes a formatted message to the log as fatal with exception details.
		/// </summary>
		/// <param name="log">The log to write to.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
        /// <param name="format">The formatted text.</param>
        /// <param name="args">The arguments for the format.</param>
		public static void Fatal(this ILog log, Exception ex, string format, params object[] args)
		{
			log.Fatal(string.Format(format, args), ex);
		}
		#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
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions