Пример #1
0
 /// <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;
 }
Пример #2
0
        /// <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())));
        }
Пример #3
0
        /// <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);
            }
        }
Пример #4
0
 /// <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;
 }
Пример #5
0
        /// <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);
            }
        }
Пример #6
0
        /// <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);
            }
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        /// <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;
        }
Пример #9
0
 /// <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.
             }
         }
     }
 }
Пример #10
0
 /// <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.
             }
         }
     }
 }
Пример #11
0
 /// <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.
             }
         }
     }
 }
Пример #12
0
        /// <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);
            }
        }
Пример #13
0
 /// <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);
 }
Пример #14
0
 /// <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);
 }
Пример #15
0
 /// <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);
 }
Пример #16
0
 /// <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);
 }
Пример #17
0
 /// <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);
 }
Пример #18
0
 /// <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));
 }
Пример #19
0
 /// <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));
 }
Пример #20
0
 /// <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
 }
Пример #21
0
        /// <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);
            }
        }
Пример #22
0
 /// <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));
 }
Пример #23
0
 /// <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));
 }
Пример #24
0
 /// <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));
 }
Пример #25
0
 /// <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);
 }
Пример #26
0
 /// <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));
 }
Пример #27
0
 /// <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));
 }