Mercurial > pub > ImplabNet
comparison Implab/Diagnostics/Trace.cs @ 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 | |
children | babe55c34931 |
comparison
equal
deleted
inserted
replaced
211:3eb3255d8cc5 | 212:a01d9df88d74 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Diagnostics; | |
4 using System.Linq; | |
5 using System.Text; | |
6 using System.Threading.Tasks; | |
7 | |
8 namespace Implab.Diagnostics { | |
9 public static class Trace<T> { | |
10 | |
11 readonly static LogChannel<TraceEvent> _channel = new LogChannel<TraceEvent>(typeof(T).Name); | |
12 | |
13 public static LogChannel<TraceEvent> Channel { | |
14 get { return _channel; } | |
15 } | |
16 | |
17 /// <summary> | |
18 /// Starts the logical operation nested to the current operation nested to the current one. | |
19 /// </summary> | |
20 [Conditional("TRACE")] | |
21 public static void StartLogicalOperation() { | |
22 TraceContext.Instance.StartLogicalOperation(); | |
23 | |
24 } | |
25 | |
26 /// <summary> | |
27 /// Starts the logical operation with the specified name, this name is usefull in logs. | |
28 /// </summary> | |
29 /// <param name="name">Name.</param> | |
30 [Conditional("TRACE")] | |
31 public static void StartLogicalOperation(string name) { | |
32 TraceContext.Instance.StartLogicalOperation(name); | |
33 } | |
34 | |
35 /// <summary> | |
36 /// Ends the logical operation and restores the previous one. | |
37 /// </summary> | |
38 [Conditional("TRACE")] | |
39 public static void EndLogicalOperation() { | |
40 var op = TraceContext.Instance.EndLogicalOperation(); | |
41 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(op, TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms", op.Name, op.Duration))); | |
42 } | |
43 | |
44 /// <summary> | |
45 /// Writes an informational message. | |
46 /// </summary> | |
47 /// <param name="format">Format.</param> | |
48 /// <param name="arguments">Arguments.</param> | |
49 [Conditional("TRACE")] | |
50 public static void Log(string format, params object[] arguments) { | |
51 Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Information, format, arguments)); | |
52 } | |
53 | |
54 /// <summary> | |
55 /// Writes a warning message. | |
56 /// </summary> | |
57 /// <param name="format">Format.</param> | |
58 /// <param name="arguments">Arguments.</param> | |
59 [Conditional("TRACE")] | |
60 public static void Warn(string format, params object[] arguments) { | |
61 Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Warning, format, arguments)); | |
62 } | |
63 | |
64 [Conditional("TRACE")] | |
65 public static void Error(string format, params object[] arguments) { | |
66 Channel.LogEvent(TraceEvent.Create(TraceContext.Instance.CurrentOperation, TraceEventType.Error, format, arguments)); | |
67 } | |
68 | |
69 [Conditional("TRACE")] | |
70 public static void Error(Exception err) { | |
71 Error("{0}", err); | |
72 } | |
73 } | |
74 } |