-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cs
201 lines (179 loc) · 5.93 KB
/
Log.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#region Using statements
using DiskSpace.Properties;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
#endregion
namespace DiskSpace
{
/// <summary>
/// Application log
/// </summary>
internal static class Log
{
#region Static variables
private static readonly string logFile = Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".log";
private static readonly string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
private static readonly string logFileFullPath = Path.Combine(appDataFolder, logFile);
#endregion
#region Internal static constructor
/// <summary>
/// Constructor - Init log
/// </summary>
static Log()
{
Init();
}
#endregion
#region Internal methods
/// <summary>
/// Init log
/// </summary>
internal static void Init()
{
try
{
Trace.Listeners.Clear();
FileStream fs = new FileStream(logFileFullPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, 1024, FileOptions.WriteThrough);
TextWriterTraceListener traceListener = new TextWriterTraceListener(fs);
Trace.Listeners.Add(traceListener);
Trace.AutoFlush = true;
Trace.UseGlobalLock = false;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
internal static void Close(string info)
{
Info = info;
Close();
}
internal static void Close()
{
Truncate();
Trace.Flush();
Trace.Close();
}
/// <summary>
/// Truncate log
/// </summary>
internal static void Truncate()
{
try
{
Trace.Flush();
Trace.Close();
Trace.Listeners.Clear();
FileInfo fi = new FileInfo(logFileFullPath);
if (fi.Exists)
{
int trimSize = Settings.Default.logFileSizeMB * 1024 * 1024;
if (fi.Length > trimSize)
{
using (MemoryStream ms = new MemoryStream(trimSize))
using (FileStream s = new FileStream(logFileFullPath, FileMode.Open, FileAccess.ReadWrite))
{
s.Seek(-trimSize, SeekOrigin.End);
byte[] bytes = new byte[trimSize];
s.Read(bytes, 0, trimSize);
ms.Write(bytes, 0, trimSize);
ms.Position = 0;
s.SetLength(trimSize);
s.Position = 0;
ms.CopyTo(s);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
Init();
}
internal static void Show(string info)
{
Info = info;
Show();
}
internal static void Show()
{
try
{
if (File.Exists(logFileFullPath))
{
using (Process process = new Process() { StartInfo = new ProcessStartInfo(logFileFullPath) { UseShellExecute = true } })
{
process.Start();
}
}
}
catch (InvalidOperationException ex)
{
Error = ex;
}
}
internal static void LogCaller()
{
StackTrace stackTrace = new StackTrace();
string method = stackTrace.GetFrame(1).GetMethod().Name;
string methodClass = stackTrace.GetFrame(1).GetMethod().DeclaringType.FullName;
string calleeMethod = stackTrace.GetFrame(2).GetMethod().Name;
string calleeClass = stackTrace.GetFrame(2).GetMethod().DeclaringType.FullName;
string infoText = $"{methodClass}::{(method == ".ctor" ? "constructor" : method)} called from {calleeClass}::{(calleeMethod == ".ctor" ? "constructor" : calleeMethod)}";
Info = infoText;
}
#endregion
#region Internal static log properties
/// <summary>
/// Log info
/// </summary>
internal static string Info
{
private get => string.Empty;
set
{
try
{
Trace.TraceInformation($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture)} {value}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
/// <summary>
/// Log error
/// </summary>
internal static Exception Error
{
private get => new ArgumentNullException(logFile);
set
{
try
{
Trace.TraceError($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture)} {value}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
/// <summary>
/// Log an error string
/// </summary>
internal static string ErrorString
{
private get => string.Empty;
set => Trace.TraceError($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture)} {value}");
}
#endregion
}
}