forked from tomasrudh/WinSize4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClsDebug.cs
More file actions
114 lines (106 loc) · 3.83 KB
/
ClsDebug.cs
File metadata and controls
114 lines (106 loc) · 3.83 KB
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
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
namespace WinSize4
{
public static class ClsDebug
{
public static bool Debug = false;
public static string _text = "";
private static string _activePath; // Will be set by frmMain at startup
//**********************************************
/// <summary>
/// Sets the active directory for logging. Must be called once at startup.
/// </summary>
//**********************************************
public static void Initialize(string path)
{
_activePath = path;
}
//**********************************************
/// <summary>
/// Checks if the path has been set. Prevents crashes.
/// </summary>
//**********************************************
private static bool IsPathInitialized()
{
if (string.IsNullOrEmpty(_activePath))
{
// Fallback to prevent crashes if Initialize() is not called.
// In a properly functioning app, this should not be hit.
_activePath = Path.GetDirectoryName(Application.ExecutablePath);
}
return true;
}
public static void ClearLog()
{
if (!IsPathInitialized()) return;
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
if (File.Exists(fullPath))
{
File.Delete(fullPath);
}
_text = "";
}
public static void AddText(string Text)
{
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
_text += dt + " " + Text + "\n";
}
public static void LogText()
{
if (!Debug || !IsPathInitialized()) return;
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
using (var writer = new StreamWriter(fullPath, true))
{
writer.WriteLine(dt + " " + _text);
}
_text = "";
}
public static void LogNow(string Text)
{
if (!Debug || !IsPathInitialized()) return;
string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
Directory.CreateDirectory(_activePath);
string fullPath = Path.Combine(_activePath, "Debug.txt");
using (var writer = new StreamWriter(fullPath, true))
{
writer.WriteLine(dt + " " + Text);
}
}
public static void LogToEvent(Exception ex, EventLogEntryType Type, string text)
{
if (ex.StackTrace == null)
{
try
{
EventLog.WriteEntry("WinSize4", text, Type, 1);
}
catch
(Exception ex2)
{
EventLog.WriteEntry("Application", text, Type, 1);
}
if (Debug)
{
Console.WriteLine(text);
}
}
else
{
StackTrace st = new StackTrace(ex, true);
StackFrame frame = st.GetFrame(0);
int line = frame.GetFileLineNumber();
EventLog.WriteEntry("WinSize4", ex.Message + "\n" + st.ToString() + text, Type, 1);
if (Debug)
{
Console.WriteLine(ex.Message + "\n" + st.ToString() + "\n" + text);
}
}
}
}
}