annotate Implab/Diagnostics/LogChannel.cs @ 65:653c4e04968b

minor changes
author cin
date Mon, 30 Jun 2014 13:55:22 +0400
parents d9d794b61bb9
children 4c0e5ef99986
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>
36
313f708a50e9 improved log concept
cin
parents:
diff changeset
30 public event EventHandler<ValueEventArgs<TEvent>> Events;
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
31
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;
313f708a50e9 improved log concept
cin
parents:
diff changeset
66 if (t!= null)
313f708a50e9 improved log concept
cin
parents:
diff changeset
67 t(TraceContext.Current,new ValueEventArgs<TEvent>(data));
313f708a50e9 improved log concept
cin
parents:
diff changeset
68 }
37
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
69
48
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
70 /// <summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
71 /// Отправляет запись журнала через канал подписчикам.
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
72 /// </summary>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
73 /// <param name="data">Запись журнала.</param>
d9d794b61bb9 Interactive tracing
cin
parents: 37
diff changeset
74 /// <param name="context">Контекст трассировки от которого рассылается сообщение/</param>
37
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
75 public void LogEvent(TraceContext context,TEvent data) {
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
76 var t = Events;
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
77 if (t != null)
c2c043520724 working version of diagnostics logging
cin
parents: 36
diff changeset
78 t(context, new ValueEventArgs<TEvent>(data));
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 }