-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyTrace.cs
138 lines (119 loc) · 3.87 KB
/
MyTrace.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
namespace Coin.Diagnostics
{
public enum TraceLevel { Level1, Level2, Level3 }
public class MyTrace
{
static MyTrace _instance;
TraceLevel TraceLevel = TraceLevel.Level1;
private static Object _thisLock = new Object();
private static MyTrace Instance
{
get
{
lock (_thisLock)
{
if (_instance == null)
_instance = new MyTrace();
}
return _instance;
}
}
public MyTrace()
{
if (ConfigurationManager.AppSettings["TracePath"] != null)
{
// Creates the text file that the trace listener will write to.
System.IO.FileStream myTraceLog = new
System.IO.FileStream(
Path.Combine(ConfigurationManager.AppSettings["TracePath"], "EmailQueueWorkerTrace"),
System.IO.FileMode.OpenOrCreate);
// Creates the new trace listener.
TextWriterTraceListener myListener =
new TextWriterTraceListener(myTraceLog);
Trace.Listeners.Add(myListener);
}
if (ConfigurationManager.AppSettings["TraceToConsole"] != null && Boolean.Parse(ConfigurationManager.AppSettings["TraceToConsole"]) == true)
{
// Creates the new trace listener.
TextWriterTraceListener myListener =
new TextWriterTraceListener(Console.Out);
Trace.Listeners.Add(myListener);
}
if (ConfigurationManager.AppSettings["TraceLevel"] != null)
{
TraceLevel = (TraceLevel)Enum.Parse(typeof(TraceLevel), ConfigurationManager.AppSettings["TraceLevel"]);
}
}
public static void WriteLine(TraceLevel level, object value)
{
string s = String.Format("{0}\t{1}\t", DateTime.Now, Thread.CurrentThread.Name) + value;
Trace.WriteLineIf(level <= Instance.TraceLevel,
value);
Trace.Flush();
}
public static void WriteLine(object value)
{
WriteLine(TraceLevel.Level1, value);
}
static SpecificLevelTrace _level1Trace;
public static SpecificLevelTrace Level1Trace
{
get
{
lock (_thisLock)
{
if (_level1Trace == null)
_level1Trace = new SpecificLevelTrace(TraceLevel.Level1);
}
return _level1Trace;
}
}
static SpecificLevelTrace _level2Trace;
public static SpecificLevelTrace Level2Trace
{
get
{
lock (_thisLock)
{
if (_level2Trace == null)
_level2Trace = new SpecificLevelTrace(TraceLevel.Level2);
}
return _level2Trace;
}
}
static SpecificLevelTrace _level3Trace;
public static SpecificLevelTrace Level3Trace
{
get
{
lock (_thisLock)
{
if (_level3Trace == null)
_level3Trace = new SpecificLevelTrace(TraceLevel.Level3);
}
return _level3Trace;
}
}
public static void Flush()
{
Trace.Flush();
}
}
public class SpecificLevelTrace
{
TraceLevel _level;
public SpecificLevelTrace(TraceLevel level)
{
_level = level;
}
public void WriteLine(object value)
{
MyTrace.WriteLine(_level, value);
}
}
}