view Implab/Diagnostics/TraceLog.cs @ 207:558f34b2fb50 v2

added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()' added Safe.Dispose(IEnumerable) added PromiseExtensions.CancellationPoint to add a cancellation point to the chain of promises added IPromise<T> PromiseExtensions.Then<T>(this IPromise<T> that, Action<T> success) overloads added PromiseExtensions.Error() overloads to handle a error or(and) a cancellation
author cin
date Wed, 09 Nov 2016 12:03:22 +0300
parents 71e543dbe65a
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Implab.Diagnostics {
    /// <summary>
    /// This class is used to trace a logical flow of the application, it publishes events to the default <see cref="TraceEvent"/> channel.
    /// </summary>
    public static class TraceLog {
        /// <summary>
        /// Starts the logical operation nested to the current operation nested to the current one.
        /// </summary>
        [Conditional("TRACE")]
        public static void StartLogicalOperation() {
            TraceContext.Instance.StartLogicalOperation();

        }

        /// <summary>
        /// Starts the logical operation with the specified name, this name is usefull in logs.
        /// </summary>
        /// <param name="name">Name.</param>
        [Conditional("TRACE")]
        public static void StartLogicalOperation(string name) {
            TraceContext.Instance.StartLogicalOperation(name);
        }

        /// <summary>
        /// Ends the logical operation and restores the previous one.
        /// </summary>
        [Conditional("TRACE")]
        public static void EndLogicalOperation() {
            var op = TraceContext.Instance.EndLogicalOperation();
            LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(op, TraceEventType.OperationCompleted,  String.Format("-{0} : {1}ms",op.Name, op.Duration)));
        }

        /// <summary>
        /// Writes an informational message.
        /// </summary>
        /// <param name="format">Format.</param>
        /// <param name="arguments">Arguments.</param>
        [Conditional("TRACE")]
        public static void TraceInformation(string format, params object[] arguments) {
            LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Information, format, arguments));
        }

        /// <summary>
        /// Writes a warning message.
        /// </summary>
        /// <param name="format">Format.</param>
        /// <param name="arguments">Arguments.</param>
        [Conditional("TRACE")]
        public static void TraceWarning(string format, params object[] arguments) {
            LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Warning, format, arguments));
        }

        [Conditional("TRACE")]
        public static void TraceError(string format, params object[] arguments) {
            LogChannel<TraceEvent>.Default.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Error, format, arguments));
        }

        [Conditional("TRACE")]
        public static void TraceError(Exception err) {
            TraceError("{0}", err);
        }
    }
}