-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
68 lines (62 loc) · 2.08 KB
/
Logger.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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace DirTemplateExtension
{
public class Logger
{
private static readonly string LogName;
static Logger()
{
LogName = Assembly.GetExecutingAssembly().FullName;
}
public static void WriteLog(string message, EventLogEntryType eventType = EventLogEntryType.Information)
{
try
{
using (var eventLog = new EventLog())
{
eventLog.Source = Configuration.EventLogSource;
eventLog.WriteEntry($"{message} [{LogName}]", eventType);
}
}
catch (Exception exception)
{
if (eventType != EventLogEntryType.Information && eventType != EventLogEntryType.SuccessAudit)
{
MessageBox.Show(string.Format(Resources.Logger_Exception, exception.Message, message),
Resources.Logger_WriteLog_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
finally
{
Debug(message);
}
}
public static void Debug(string message)
{
var templateDirPath = Configuration.GetTemplateDirectoryInfo().FullName;
if (!File.Exists(Path.Combine(templateDirPath, ".debug")))
{
return;
}
var logFile = Path.Combine(templateDirPath, "debug.log");
try
{
var append = File.Exists(logFile) && new FileInfo(logFile).Length < 10 * 1024 * 1024;
using (var logWriter = new StreamWriter(logFile, append))
{
logWriter.WriteLine(DateTime.Now + "> " + message);
logWriter.Flush();
}
}
catch (Exception exception)
{
// ignored
MessageBox.Show(exception.ToString());
}
}
}
}