changeset 212:a01d9df88d74 v2

Added class Trace<T> to manage channels for individual classes, if SomeClass uses Trace<SomeClass> it sould be marked with TraceSourceAttribute
author cin
date Tue, 04 Apr 2017 12:04:05 +0300
parents 3eb3255d8cc5
children 9ee78a345738
files Implab/Diagnostics/Trace.cs Implab/Diagnostics/TraceSourceAttribute.cs Implab/Implab.csproj
diffstat 3 files changed, 86 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/Diagnostics/Trace.cs	Tue Apr 04 12:04:05 2017 +0300
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Implab.Diagnostics {
+    public static class Trace<T> {
+
+        readonly static LogChannel<TraceEvent> _channel = new LogChannel<TraceEvent>(typeof(T).Name);
+
+        public static LogChannel<TraceEvent> Channel {
+            get { return _channel; }
+        }
+        
+        /// <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 Log(string format, params object[] arguments) {
+            Channel.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 Warn(string format, params object[] arguments) {
+            Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Warning, format, arguments));
+        }
+
+        [Conditional("TRACE")]
+        public static void Error(string format, params object[] arguments) {
+            Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Error, format, arguments));
+        }
+
+        [Conditional("TRACE")]
+        public static void Error(Exception err) {
+            Error("{0}", err);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/Diagnostics/TraceSourceAttribute.cs	Tue Apr 04 12:04:05 2017 +0300
@@ -0,0 +1,10 @@
+using System;
+
+namespace Implab.Diagnostics {
+    /// <summary>
+    /// Used to mark class which uses <see cref="Trace{T}"/> class to trace it's events
+    /// </summary>
+    [AttributeUsage(AttributeTargets.Class)]
+    public class TraceSourceAttribute : Attribute {
+    }
+}
--- a/Implab/Implab.csproj	Tue Mar 21 17:29:13 2017 +0300
+++ b/Implab/Implab.csproj	Tue Apr 04 12:04:05 2017 +0300
@@ -81,9 +81,11 @@
     <Compile Include="Diagnostics\LogChannel.cs" />
     <Compile Include="Diagnostics\LogicalOperation.cs" />
     <Compile Include="Diagnostics\TextFileListener.cs" />
+    <Compile Include="Diagnostics\Trace.cs" />
     <Compile Include="Diagnostics\TraceLog.cs" />
     <Compile Include="Diagnostics\TraceEvent.cs" />
     <Compile Include="Diagnostics\TraceEventType.cs" />
+    <Compile Include="Diagnostics\TraceSourceAttribute.cs" />
     <Compile Include="ICancellable.cs" />
     <Compile Include="IProgressHandler.cs" />
     <Compile Include="IProgressNotifier.cs" />