133 lines
3.6 KiB
C#
133 lines
3.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.Kernel.Structs;
|
|
|
|
namespace JD.LogService
|
|
{
|
|
/// <summary>
|
|
/// Represents an error or a warning.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class Error : LogEvent
|
|
{
|
|
#region Variables
|
|
|
|
private string m_code = string.Empty;
|
|
private object m_tag;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public Error(LogCategory category)
|
|
: base(category)
|
|
{
|
|
Throw.InvalidArgumentIf(category == LogCategory.Message, "Message is not considered to be a valid error category", "category");
|
|
}
|
|
|
|
public Error(LogCategory category, string message)
|
|
: this(category)
|
|
{
|
|
Message = message;
|
|
}
|
|
|
|
public Error(LogCategory category, string message, params object[] args)
|
|
: this(category, FormatString(message, args))
|
|
{
|
|
}
|
|
|
|
[Obsolete("Log events are now mutable. Use the basic Error(LogCategory, string) constructor and set the rest of the properties. Subcategory is now obsolete.")] // 2011.01
|
|
public Error(LogCategory category, FileLocation origin, string message, string subCategory, string code)
|
|
: this(category, message)
|
|
{
|
|
Origin = origin;
|
|
Code = code;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Static Creation
|
|
|
|
/// <summary>
|
|
/// Creates an error that wraps the given exception.
|
|
/// </summary>
|
|
/// <param name="ex">Exception that caused the error.</param>
|
|
/// <param name="message">Message</param>
|
|
/// <param name="args">Args</param>
|
|
/// <returns></returns>
|
|
public static Error FromException(Exception ex, string message, params object[] args)
|
|
{
|
|
return new Error(LogCategory.Error, message, args);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Code of the error, if any.
|
|
/// </summary>
|
|
public string Code
|
|
{
|
|
get { return m_code; }
|
|
set { m_code = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prefix used before the location part (ex.: 3> C:\file.txt)
|
|
/// </summary>
|
|
public string Prefix { get; set; }
|
|
|
|
/// <summary>
|
|
/// An opaque tag that can be used to store additional info about the error.
|
|
/// </summary>
|
|
public object Tag
|
|
{
|
|
get { return m_tag; }
|
|
set { m_tag = value; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Returns the error in canonical format.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToCanonicalString()
|
|
{
|
|
return CanonicalError.Build(this);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Message.GetHashCode();
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
Error toCompare = obj as Error;
|
|
|
|
return toCompare != null &&
|
|
Equals((LogEvent)obj) &&
|
|
m_code == toCompare.m_code &&
|
|
m_tag == toCompare.m_tag;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// For a canonical error string, you might want to take a look at <see cref="ToCanonicalString"/>
|
|
/// </remarks>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return string.Format(CultureInfo.CurrentCulture, "[{0}: Message={1}, Origin={2}, Code={3}]", Category, Message, Origin, Code);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|