/// <summary> /// Constructs a log entry. /// </summary> /// <param name="type">The entry type.</param> /// <param name="message">The message.</param> /// <param name="extension">The extended log information (or <c>null</c>).</param> public SysLogEntry(ISysLogEntryExtension extension, SysLogEntryType type, string message) { this.Time = DateTime.UtcNow; this.Type = type; this.Message = message; this.Extension = extension; }
/// <summary> /// Computes an MD5 hash code for the log entry /// </summary> /// <param name="extension">The extended entry information (or <c>null</c>).</param> /// <param name="entryType">The log entry type.</param> /// <param name="message">The log message (or <c>null</c>).</param> /// <param name="e">The logged exception (or <c>null</c>).</param> /// <returns>The computed hash.</returns> private static MD5Key ComputeMD5Hash(ISysLogEntryExtension extension, SysLogEntryType entryType, string message, Exception e) { StringBuilder sb = new StringBuilder(1024); if (extension != null) { sb.Append(extension.Format()); } sb.Append('\t'); sb.Append(entryType.ToString()); sb.Append('\t'); if (message != null) { sb.Append(message); } sb.Append('\t'); if (e != null) { sb.Append(e.GetType().FullName); sb.Append('\t'); sb.Append(e.Message); sb.Append('\t'); sb.Append(e.StackTrace.ToString()); } return(new MD5Key(MD5Hasher.Compute(sb.ToString()))); }
/// <summary> /// Logs an error, including a dump of the current stack and with /// optional extended log information. /// </summary> /// <param name="extension">Extended log information (or <c>null</c>).</param> /// <param name="format">The message format string.</param> /// <param name="args">The formatting arguments.</param> public static void LogErrorStackDump(ISysLogEntryExtension extension, string format, params object[] args) { if (provider == null) { return; } StringBuilder sb = new StringBuilder(); string message; if (args.Length > 0) { sb.AppendFormat(format, args); } else { sb.Append(format); } sb.Append("\r\nStack:\r\n\r\n"); sb.Append(new CallStack(1, true).ToString()); message = sb.ToString(); if (!IsCached(extension, SysLogEntryType.Error, message, null)) { provider.LogError(extension, message); } }
/// <summary> /// Constructor. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="entryType">Indicates the log entry type.</param> /// <param name="message">The log message (or <c>null</c>).</param> /// <param name="e">The exception (or <c>null</c>).</param> public CachedEntry(ISysLogEntryExtension extension, SysLogEntryType entryType, string message, Exception e) { this.EntryType = entryType; this.Extension = extension; this.Message = message; this.Exception = e; this.FirstTime = this.LastTime = DateTime.UtcNow; this.TTD = SysTime.Now + cacheTime; this.Count = 1; }
/// <summary> /// Logs an informational entry with optional extended log information. /// </summary> /// <param name="extension">Extended log information (or <c>null</c>).</param> /// <param name="format">The message format string.</param> /// <param name="args">The formatting arguments.</param> public static void LogInformation(ISysLogEntryExtension extension, string format, params object[] args) { if (provider == null) { return; } string message = args.Length > 0 ? string.Format(format, args) : format; if (!IsCached(extension, SysLogEntryType.Information, message, null)) { provider.LogInformation(extension, message); } }
/// <summary> /// Logs an exception with optional extended log information. /// </summary> /// <param name="extension">Extended log information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> public static void LogException(ISysLogEntryExtension extension, Exception e) { if (provider == null) { return; } if (e is ThreadAbortException) { return; // These really aren't errors. } if (!IsCached(extension, SysLogEntryType.Exception, null, e)) { provider.LogException(extension, e); } }
/// <summary> /// Logs debugging related information with optional extended information. /// </summary> /// <param name="extension">Extended log information (or <c>null</c>).</param> /// <param name="component">Identifies the component described by the trace.</param> /// <param name="level">Specifies the relative importance of the trace.</param> /// <param name="format">The message format string.</param> /// <param name="args">The formatting arguments.</param> public static void Trace(ISysLogEntryExtension extension, string component, SysLogLevel level, string format, params object[] args) { if (level != SysLogLevel.High) { return; } if (provider == null) { return; } var message = args.Length > 0 ? string.Format(format, args) : format; provider.Trace(extension, component, message); }
/// <summary> /// Constucts an exception based log entry. /// </summary> /// <param name="extension">The extended log information (or <c>null</c>).</param> /// <param name="e">The exception.</param> /// <param name="message">The message.</param> public SysLogEntry(ISysLogEntryExtension extension, Exception e, string message) { TargetInvocationException eInvoke; eInvoke = e as TargetInvocationException; if (eInvoke != null) { e = eInvoke.InnerException; } this.Time = DateTime.UtcNow; this.Type = SysLogEntryType.Exception; this.Exception = e; this.Message = message; this.Extension = extension; }
/// <summary> /// Logs debugging related information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="category">Used to group debugging related log entries.</param> /// <param name="message">The message.</param> public void Trace(ISysLogEntryExtension extension, string category, string message) { lock (syncLock) { foreach (var provider in providers) { try { provider.Trace(extension, category, message); } catch { // Ignore errors. } } } }
/// <summary> /// Logs an exception with additional information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> /// <param name="message">The message.</param> public void LogException(ISysLogEntryExtension extension, Exception e, string message) { lock (syncLock) { foreach (var provider in providers) { try { provider.LogException(extension, e, message); } catch { // Ignore errors. } } } }
/// <summary> /// Logs a failed security related change or access attempt. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogSecurityFailure(ISysLogEntryExtension extension, string message) { lock (syncLock) { foreach (var provider in providers) { try { provider.LogSecurityFailure(extension, message); } catch { // Ignore errors. } } } }
/// <summary> /// Logs an exception with an additional message and /// optional extended log information. /// </summary> /// <param name="extension">Extended log information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> /// <param name="format">The message format string.</param> /// <param name="args">The formatting arguments.</param> public static void LogException(ISysLogEntryExtension extension, Exception e, string format, params object[] args) { if (provider == null) { return; } if (e is ThreadAbortException) { return; // These really aren't errors. } var message = args.Length > 0 ? string.Format(format, args) : format; if (!IsCached(extension, SysLogEntryType.Exception, message, e)) { provider.LogException(extension, e, message); } }
/// <summary> /// Logs a warning. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogWarning(ISysLogEntryExtension extension, string message) { Switch.Log(MapLogLevel(SysLogEntryType.Warning), message); }
/// <summary> /// Logs an informational entry. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogInformation(ISysLogEntryExtension extension, string message) { Switch.Log(MapLogLevel(SysLogEntryType.Information), message); }
/// <summary> /// Logs debugging related information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="category">Used to group debugging related log entries.</param> /// <param name="message">The message.</param> public void Trace(ISysLogEntryExtension extension, string category, string message) { Switch.Log(MapLogLevel(SysLogEntryType.Trace), message); }
/// <summary> /// Logs a failed security related change or access attempt. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogSecurityFailure(ISysLogEntryExtension extension, string message) { Switch.Log(MapLogLevel(SysLogEntryType.SecurityFailure), message); }
/// <summary> /// Logs an exception with additional information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> /// <param name="message">The message.</param> public void LogException(ISysLogEntryExtension extension, Exception e, string message) { Switch.Log(MapLogLevel(SysLogEntryType.Exception), "Exception [{0}] : {1}", e.GetType().Name, e.Message); }
/// <summary> /// Logs an exception. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> public void LogException(ISysLogEntryExtension extension, Exception e) { Append(new SysLogEntry(extension, e, null)); }
/// <summary> /// Logs an informational entry. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogInformation(ISysLogEntryExtension extension, string message) { Append(new SysLogEntry(extension, SysLogEntryType.Information, message)); }
/// <summary> /// Handles the caching for a log entry. /// </summary> /// <param name="extension">Extended log entry information or (<c>null</c>).</param> /// <param name="entryType">The log entry type.</param> /// <param name="message">The log message (or <c>null</c>).</param> /// <param name="e">The logged exception (or <c>null</c>).</param> /// <returns><c>true</c> if the entry is in the cache and should not be logged.</returns> private static bool IsCached(ISysLogEntryExtension extension, SysLogEntryType entryType, string message, Exception e) { return(false); // Caching is not implemented for Silverlight }
/// <summary> /// Handles the caching for a log entry. /// </summary> /// <param name="extension">Extended log entry information or (<c>null</c>).</param> /// <param name="entryType">The log entry type.</param> /// <param name="message">The log message (or <c>null</c>).</param> /// <param name="e">The logged exception (or <c>null</c>).</param> /// <returns><c>true</c> if the entry is in the cache and should not be logged.</returns> private static bool IsCached(ISysLogEntryExtension extension, SysLogEntryType entryType, string message, Exception e) { lock (cache) { if (cacheTime == TimeSpan.Zero) { return(false); } switch (entryType) { case SysLogEntryType.Trace: if (!SysLog.CacheDebug) { return(false); } break; case SysLogEntryType.Error: if (!SysLog.CacheErrors) { return(false); } break; case SysLogEntryType.Exception: if (!SysLog.CacheExceptions) { return(false); } break; case SysLogEntryType.Information: if (!SysLog.CacheInformation) { return(false); } break; case SysLogEntryType.SecurityFailure: case SysLogEntryType.SecuritySuccess: if (!SysLog.CacheSecurity) { return(false); } break; case SysLogEntryType.Warning: if (!SysLog.CacheWarnings) { return(false); } break; default: Debug.WriteLine("Unexpected log entry type."); break; } MD5Key key = ComputeMD5Hash(extension, entryType, message, e); CachedEntry entry; if (cache.TryGetValue(key, out entry)) { entry.Count++; entry.LastTime = DateTime.UtcNow; return(true); } entry = new CachedEntry(extension, entryType, message, e); cache.Add(key, entry); return(false); } }
/// <summary> /// Logs a warning. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogWarning(ISysLogEntryExtension extension, string message) { Append(new SysLogEntry(extension, SysLogEntryType.Warning, message)); }
/// <summary> /// Logs debugging related information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="category">Used to group debugging related log entries.</param> /// <param name="message">The message.</param> public void Trace(ISysLogEntryExtension extension, string category, string message) { Append(new SysLogEntry(extension, SysLogEntryType.Trace, category, message)); }
/// <summary> /// Logs an error. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogError(ISysLogEntryExtension extension, string message) { Append(new SysLogEntry(extension, SysLogEntryType.Error, message)); }
/// <summary> /// Logs an error. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogError(ISysLogEntryExtension extension, string message) { Switch.Log(MapLogLevel(SysLogEntryType.Error), message); }
/// <summary> /// Logs a failed security related change or access attempt. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="message">The message.</param> public void LogSecurityFailure(ISysLogEntryExtension extension, string message) { Append(new SysLogEntry(extension, SysLogEntryType.SecurityFailure, message)); }
/// <summary> /// Logs an exception with additional information. /// </summary> /// <param name="extension">Extended log entry information (or <c>null</c>).</param> /// <param name="e">The exception being logged.</param> /// <param name="message">The message.</param> public void LogException(ISysLogEntryExtension extension, Exception e, string message) { Append(new SysLogEntry(extension, e, message)); }