using System; using System.Globalization; using JD.Kernel.StaticHelpers; using JD.Kernel.Structs; namespace JD.LogService { /// /// Represents an error or a warning. /// [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 /// /// Creates an error that wraps the given exception. /// /// Exception that caused the error. /// Message /// Args /// public static Error FromException(Exception ex, string message, params object[] args) { return new Error(LogCategory.Error, message, args); } #endregion #region Properties /// /// Code of the error, if any. /// public string Code { get { return m_code; } set { m_code = value; } } /// /// Prefix used before the location part (ex.: 3> C:\file.txt) /// public string Prefix { get; set; } /// /// An opaque tag that can be used to store additional info about the error. /// public object Tag { get { return m_tag; } set { m_tag = value; } } #endregion #region Methods /// /// Returns the error in canonical format. /// /// 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; } /// /// Overriden. /// /// /// For a canonical error string, you might want to take a look at /// /// public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "[{0}: Message={1}, Origin={2}, Code={3}]", Category, Message, Origin, Code); } #endregion } }