#region  --------------- Copyright Dresser Wayne Pignone -------------
/*
 * $Log: /Wrk/WayneLibraries/Wrk/Log/ExceptionLogEntry.cs $
 * 
 * 3     08-02-13 9:26 Mattias.larsson
 * FxCop fixes.
 */
#endregion

using System;
using System.Text;
using System.Diagnostics.CodeAnalysis;
namespace Wayne.Lib.Log
{
    /// <summary>
    /// Log entry for exception errors.
    /// </summary>
    public class ExceptionLogEntry : ErrorLogEntry
    {
        #region Fields

        private Exception exception;

        #endregion

        #region Construction

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="severity"></param>
        /// <param name="logObject">The object to log.</param>
        /// <param name="exception"></param>
        [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
        public ExceptionLogEntry(IIdentifiableEntity entity, ErrorLogSeverity severity, object logObject, Exception exception)
            : base(entity, severity, logObject)
        {
            this.exception = exception;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="severity"></param>
        /// <param name="logObject">The object to log.</param>
        /// <param name="category"></param>
        /// <param name="exception"></param>
        [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
        public ExceptionLogEntry(IIdentifiableEntity entity, ErrorLogSeverity severity, object logObject, object category, Exception exception)
            : base(entity, severity, logObject, category)
        {
            this.exception = exception;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Exception information.
        /// </summary>
        public Exception Exception
        {
            get { return exception; }
        }

        #endregion

        #region Methods: AppendTextToStringBuilder

        /// <summary>
        /// Appends the object to log to a StringBuilder-output.
        /// </summary>
        /// <param name="logWriter">The logwriter to be used for logging.</param>
        /// <param name="output">The StringBuilder.</param>
        /// <param name="indentLength">The indent to be used if many lines.</param>
        /// <param name="isFirstLine">Is this the first line to log?</param>
        /// <param name="indent">A string holding a generated indent-text (=a number of spaces). Use AppendIndent() to append the indent.</param>
        internal override void AppendToStringBuilder(LogWriter logWriter, StringBuilder output,
            int indentLength, ref bool isFirstLine, ref string indent)
        {
            base.AppendToStringBuilder(logWriter, output, indentLength, ref isFirstLine, ref indent);

            // Add the exception.
            if (exception != null)
            {
                StringLogObject.EnsureIndent(indentLength, ref indent);
                string headerText = "Exception: ";
                Exception ex = exception;
                while (ex != null)
                {
                    StringLogObject.AppendStringToStringBuilder(headerText + ex.ToString(), output, logWriter, indentLength, ref isFirstLine, ref indent);

                    // Check if there is an inner exception
                    ex = ex.InnerException;
                    if (ex != null)
                    {
                        // Indent 3 more spaces.
                        indent += "   ";
                        headerText = "Inner Exception: ";
                    }
                }

                
                
                //StringLogObject.AppendStringToStringBuilder("Exception: ", output, logWriter, indentLength, true, ref indent);

                //string newLineString = "\r\n" + Wayne.Lib.Strings.Indent(indentLength, true);
                //output.Append(newLineString);
                //output.Append("Exception: ");

                //Exception ex = exception;
                //while (ex != null)
                //{
                //    StringLogObject.AppendObjectToStringBuilder(ex.Message, output, logWriter, indentLength, false, ref indent);

                //    // Check if there is an inner exception
                //    ex = ex.InnerException;
                //    if (ex != null)
                //    {
                //        // Indent 3 more spaces.
                //        newLineString += "   ";
                //        indentLength += 3;

                //        output.Append(newLineString);
                //        output.Append("Inner Exception: ");
                //    }
                //}
            }
        }

        #endregion
    }
}