forked from hughpyle/inguz-DSPUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrace.cs
144 lines (132 loc) · 4 KB
/
Trace.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
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
// Copyright (c) 2006, 2009 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
/// <summary>
/// Logging of trace messages: Trace.Write(...)
///
/// Trace messgaes go to one of the three possible outputs:
/// - some TextWriter you preprared earlier, or
/// - the console, or
/// - a text file on disk. The location of the text file is set in the app's config, "trace" value.
/// </summary>
public class Trace
{
private static bool _init; // = false;
private static readonly object _lock = new Object();
private static bool _useConsole; // = false;
public static bool UseConsole
{
get { return _useConsole; }
set { _useConsole = value; }
}
private static TextWriter _useTextWriter = null;
public static TextWriter UseTextWriter
{
get { return _useTextWriter; }
set { _useTextWriter = value; }
}
private static string _tracefile; // = null;
public static string FilePath
{
set
{
_tracefile = value;
_init = true;
}
}
private static string _prefix = "";
/// <summary>
/// Prefix string for each message, only used with textfile trace (e.g. to disambiguate across processes)
/// </summary>
public static string Prefix
{
set
{
_prefix = value;
}
}
private static bool TraceFile()
{
lock (_lock)
{
if (!_useConsole && !_init)
{
try
{
System.Configuration.AppSettingsReader rdr = new System.Configuration.AppSettingsReader();
_tracefile = (string)rdr.GetValue("trace", typeof(string));
}
catch (Exception)
{
}
}
_init = true;
}
return (_init && _tracefile!=null);
}
/// <summary>
/// Write a trace message.
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void Write(string format, params object[] args)
{
// System.Diagnostics.Trace.WriteLine(String.Format(format, args));
if (_useTextWriter != null)
{
_useTextWriter.WriteLine(format, args);
}
else if (_useConsole)
{
Console.Write(format, args);
}
else if (TraceFile())
{
lock (_lock)
{
try
{
string msg = String.Format(format, args);
File.AppendAllText(_tracefile, DateTime.Now.ToString("yyyyMMddHHmmss: ") + _prefix + msg);
}
catch (Exception)
{
}
}
}
}
/// <summary>
/// Write a trace newline.
/// </summary>
public static void WriteLine()
{
Trace.Write(System.Environment.NewLine);
}
/// <summary>
/// Write a trace message and newline.
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void WriteLine(string format, params object[] args)
{
Trace.Write(format + System.Environment.NewLine, args);
}
public static void Clear()
{
if (TraceFile())
{
try
{
System.IO.File.Delete(_tracefile);
}
catch (Exception)
{
}
}
}
}
}