Click here to Skip to main content
15,882,017 members
Articles / Desktop Programming / WPF

DataObjectBase

Rate me:
Please Sign up or sign in to vote.
4.66/5 (20 votes)
18 May 2010CPOL8 min read 37.7K   572   59  
DataObjectBase -> the new Object class for data objects!
using System;
using System.Diagnostics;
using System.Globalization;

namespace CatenaLogic.Diagnostics
{
    /// <summary>
    /// Exception helper class.
    /// </summary>
    public static class ExceptionHelper
    {
        #region Methods
		/// <summary>
		/// Traces an error message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void TraceExceptionAsError(Exception ex, string message)
        {
            TraceException(TraceLevel.Error, ex, message, new object[] {});
        }

		/// <summary>
		/// Traces an error message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="format">A format string that contains zero or more format items, which correspond to objects in the args array.</param>
		/// <param name="args">An object array containing zero or more objects to format.</param>
		public static void TraceExceptionAsError(Exception ex, string format, params object[] args)
		{
			TraceException(TraceLevel.Error, ex, format, args);
		}

		/// <summary>
		/// Traces a warning message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
        public static void TraceExceptionAsWarning(Exception ex, string message)
        {
            TraceException(TraceLevel.Warning, ex, message, new object[] {});
        }

		/// <summary>
		/// Traces a warning message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="format">A format string that contains zero or more format items, which correspond to objects in the args array.</param>
		/// <param name="args">An object array containing zero or more objects to format.</param>
		public static void TraceExceptionAsWarning(Exception ex, string format, params object[] args)
		{
			TraceException(TraceLevel.Warning, ex, format, args);
		}

		/// <summary>
		/// Traces an information message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
        public static void TraceExceptionAsInformation(Exception ex, string message)
        {
            TraceException(TraceLevel.Info, ex, message, new object[] {});
        }

		/// <summary>
		/// Traces an information message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="format">A format string that contains zero or more format items, which correspond to objects in the args array.</param>
		/// <param name="args">An object array containing zero or more objects to format.</param>
		public static void TraceExceptionAsInformation(Exception ex, string format, params object[] args)
		{
			TraceException(TraceLevel.Info, ex, format, args);
		}

		/// <summary>
		/// Traces a verbose message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
		public static void TraceExceptionAsVerbose(Exception ex, string message)
        {
            TraceException(TraceLevel.Verbose, ex, message, new object[] {});
        }

		/// <summary>
		/// Traces a verbose message with exception details.
		/// </summary>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="format">A format string that contains zero or more format items, which correspond to objects in the args array.</param>
		/// <param name="args">An object array containing zero or more objects to format.</param>
		public static void TraceExceptionAsVerbose(Exception ex, string format, params object[] args)
		{
			TraceException(TraceLevel.Verbose, ex, format, args);
		}

		/// <summary>
		/// Traces a message with exception details.
		/// </summary>
		/// <param name="level"><see cref="TraceLevel"/> to write.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="message">Message of the trace.</param>
        public static void TraceException(TraceLevel level, Exception ex, string message)
        {
			TraceException(level, ex, message, new object[] {});
        }

		/// <summary>
		/// Traces a message with exception details.
		/// </summary>
		/// <param name="level"><see cref="TraceLevel"/> to write.</param>
		/// <param name="ex"><see cref="Exception"/> that contains the additional information.</param>
		/// <param name="format">A format string that contains zero or more format items, which correspond to objects in the args array.</param>
		/// <param name="args">An object array containing zero or more objects to format.</param>
		public static void TraceException(TraceLevel level, Exception ex, string format, params object[] args)
		{
			// Set up final message
			string message = (args.Length == 0) ? format : string.Format(CultureInfo.CurrentUICulture, format, args);
            string deepestExceptionMessage = (ex.InnerException != null ? GetInnerException(ex).Message : "null");
			string finalMessage = string.Format("{0}  -------------> (Details: '{1}' | InnerException: {2})", message,
				ex.Message, deepestExceptionMessage);

			// Now trace
			TraceHelper.TraceWithDetails(level, finalMessage, ex.ToString());
		}

        /// <summary>
        /// Gets the inner exception.
        /// </summary>
        /// <param name="ex">The ex.</param>
        private static Exception GetInnerException(Exception ex)
        {
            if (ex.InnerException != null)
                return GetInnerException(ex.InnerException);
            else
                return ex;
        }
        #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