annotate Implab/Diagnostics/LogChannel.cs @ 185:822aab37b107 ref20160224

runnable component, work in progress
author cin
date Mon, 18 Apr 2016 16:41:17 +0300
parents 04d4c92d0f28
children d45bdf510514
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
1 using System;
313f708a50e9 improved log concept
cin
parents:
diff changeset
2 using System.Collections.Generic;
313f708a50e9 improved log concept
cin
parents:
diff changeset
3 using System.Linq;
313f708a50e9 improved log concept
cin
parents:
diff changeset
4 using System.Text;
313f708a50e9 improved log concept
cin
parents:
diff changeset
5
313f708a50e9 improved log concept
cin
parents:
diff changeset
6 namespace Implab.Diagnostics {
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
7 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
8 /// Канал, через который публикуются события журнала.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
9 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
10 /// <typeparam name="TEvent">Тип событий в канале</typeparam>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
11 /// <remarks>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
12 /// Событиями журнала могут быть любые типы, например строки, в которых будет передаваться
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
13 /// информация, или структуры с набором полей, описывающих важность, текст и другую информацию.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
14 /// </remarks>
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
15 public class LogChannel<TEvent> {
313f708a50e9 improved log concept
cin
parents:
diff changeset
16 static LogChannel<TEvent> _default = new LogChannel<TEvent>();
313f708a50e9 improved log concept
cin
parents:
diff changeset
17
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
18 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
19 /// Канал по-умолчанию для событий типа <typeparam name="TEvent"/>.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
20 /// </summary>
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
21 public static LogChannel<TEvent> Default {
313f708a50e9 improved log concept
cin
parents:
diff changeset
22 get {
313f708a50e9 improved log concept
cin
parents:
diff changeset
23 return _default;
313f708a50e9 improved log concept
cin
parents:
diff changeset
24 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
25 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
26
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
27 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
28 /// Событие появление новой записи в журнале, на это событие подписываются слушатели.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
29 /// </summary>
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
30 public event EventHandler<LogEventArgs<TEvent>> Events;
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
31
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
32 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
33 /// Имя канала, полезно для отображения в журнале
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
34 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
35 public string Name {
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
36 get;
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
37 private set;
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
38 }
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
39
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
40 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
41 /// Создает журнал, имя типа событий назначается в качетве имени канала.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
42 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
43 public LogChannel()
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
44 : this(null) {
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
45 }
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
46
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
47 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
48 /// Содает канал с указанным именем.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
49 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
50 /// <param name="name">Имя канала.</param>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
51 public LogChannel(string name) {
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
52 if (String.IsNullOrEmpty(name))
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
53 name = typeof(TEvent).Name;
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
54 Name = name;
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
55 }
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
56
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
57 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
58 /// Отправляет запись журнала через канал подписчикам.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
59 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
60 /// <param name="data">Запись журнала.</param>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
61 /// <remarks>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
62 /// Контекст трассировки от которого рассылается сообщение определяется автоматически из текущего потока.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
63 /// </remarks>
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
64 public void LogEvent(TEvent data) {
313f708a50e9 improved log concept
cin
parents:
diff changeset
65 var t = Events;
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
66 if (t != null) {
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
67 var traceContext = TraceContext.Instance;
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
68 t(
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
69 this,
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
70 new LogEventArgs<TEvent>(
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
71 data,
134
04d4c92d0f28 Improved logging
cin
parents: 92
diff changeset
72 Name,
92
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
73 traceContext.ThreadId,
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
74 traceContext.CurrentOperation,
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
75 traceContext.CurrentOperation.Duration
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
76 )
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
77 );
4c0e5ef99986 rewritten tracing
cin
parents: 48
diff changeset
78 }
37
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
79 }
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
80 }
313f708a50e9 improved log concept
cin
parents:
diff changeset
81 }