#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
{
///
/// Log entry for exception errors.
///
public class ExceptionLogEntry : ErrorLogEntry
{
#region Fields
private Exception exception;
#endregion
#region Construction
///
/// Constructor.
///
///
///
/// The object to log.
///
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
public ExceptionLogEntry(IIdentifiableEntity entity, ErrorLogSeverity severity, object logObject, Exception exception)
: base(entity, severity, logObject)
{
this.exception = exception;
}
///
/// Constructor.
///
///
///
/// The object to log.
///
///
[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
///
/// Exception information.
///
public Exception Exception
{
get { return exception; }
}
#endregion
#region Methods: AppendTextToStringBuilder
///
/// Appends the object to log to a StringBuilder-output.
///
/// The logwriter to be used for logging.
/// The StringBuilder.
/// The indent to be used if many lines.
/// Is this the first line to log?
/// A string holding a generated indent-text (=a number of spaces). Use AppendIndent() to append the indent.
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
}
}