-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggerEventLog.cs
226 lines (201 loc) · 9.32 KB
/
LoggerEventLog.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Management;
using System.ComponentModel;
namespace TestAsyncLog
{
/// <summary>
/// Logger to trace potential exception (or any debug/info information) into Windows event log WHEN errors occured during asynchnously logging process into a text file
/// If all log processes into text file complete sucessfully, Windows event log will be empty.
/// </summary>
public class LoggerEventLog
{
private string _sMsgErrorLogging;
private string _sDateTimeLog;
private string _sIDLog;
private string _sTraceHeader;
private string _sTrace;
private bool _bMutextContext;
private string _sShortMutexName;
private string _sFullMutexName;
private Exception _ex;
private string _sExInfoMaxAttempt;
private string _sLogAssemblyName;
private static Object _lockObj = new Object();
public LoggerEventLog(string sMsgErrorLogging, string sDateTimeLog, string sIDLog, string sTraceHeader, string sTrace, string sLogAssemblyName) : this(sMsgErrorLogging, sDateTimeLog, sIDLog, null, sTraceHeader, sTrace, sLogAssemblyName)
{
}
public LoggerEventLog(string sMsgErrorLogging, string sDateTimeLog, string sIDLog, Exception ex, string sTraceHeader, string sTrace, string sLogAssemblyName)
{
MsgErrorLogging = sMsgErrorLogging;
DateTimeLog = sDateTimeLog;
IDLog = sIDLog;
Ex = ex;
TraceHeader = sTraceHeader;
Trace = sTrace;
SetMutextContext(false);
SetShortMutexName(String.Empty);
SetFullMutexName(String.Empty);
SetExInfoMaxAttempt(String.Empty);
LogAssemblyName = sLogAssemblyName;
}
public void Log()
{
System.Diagnostics.Debug.WriteLine(MsgErrorLogging);
StringBuilder sbExInfo = new StringBuilder();
AddStdLogInfoToSB(sbExInfo, DateTimeLog, IDLog, Ex, TraceHeader, Trace);
if (IsMutextContext == true)
{
sbExInfo.AppendLine(" [Short Mutex name: " + (String.IsNullOrEmpty(ShortMutexName) ? "Not available!" : ShortMutexName) + "]");
sbExInfo.AppendLine(" [Full Mutex name: " + (String.IsNullOrEmpty(FullMutexName) ? "Not available!" : FullMutexName) + "]");
}
if (!String.IsNullOrEmpty(ExInfoMaxAttempt))
{
sbExInfo.AppendLine(ExInfoMaxAttempt);
}
LogIntoEventLog(MsgErrorLogging, sbExInfo.ToString());
}
public string MsgErrorLogging { get => _sMsgErrorLogging; set => _sMsgErrorLogging = value; }
public string DateTimeLog { get => _sDateTimeLog; set => _sDateTimeLog = value; }
public string IDLog { get => _sIDLog; set => _sIDLog = value; }
public Exception Ex { get => _ex; set => _ex = value; }
public string TraceHeader { get => _sTraceHeader; set => _sTraceHeader = value; }
public string Trace { get => _sTrace; set => _sTrace = value; }
public string ShortMutexName { get => _sShortMutexName; }
public string FullMutexName { get => _sFullMutexName; }
public string ExInfoMaxAttempt { get => _sExInfoMaxAttempt; }
public string LogAssemblyName { get => _sLogAssemblyName; set => _sLogAssemblyName = value; }
public bool IsMutextContext { get => _bMutextContext; }
public void SetMutextContext(bool bMutextContext)
{
_bMutextContext = bMutextContext;
}
public void SetExInfoMaxAttempt(string sExInfoMaxAttempt)
{
_sExInfoMaxAttempt = sExInfoMaxAttempt;
}
public void SetShortMutexName(string sShortMutexName)
{
if (IsMutextContext == false)
{
SetMutextContext(true);
}
_sShortMutexName = sShortMutexName;
}
public void SetFullMutexName(string sFullMutexName)
{
if (IsMutextContext == false)
{
SetMutextContext(true);
}
_sFullMutexName = sFullMutexName;
}
private void AddStdLogInfoToSB(StringBuilder sbExInfo, string sDateTimeLog, string sIDLog, Exception ex, string sTraceHeader, string sTrace)
{
// Informations de trace destinées à l'EventLog
sbExInfo.AppendLine(" ");
if (!String.IsNullOrEmpty(sDateTimeLog) && !String.IsNullOrEmpty(sIDLog))
{
sbExInfo.AppendLine(" [DateTime (yyyy/MM/dd HH:mm:ss.fff) ID Log: " + $"{sDateTimeLog} {sIDLog}" + "]");
}
else
{
sbExInfo.AppendLine(" [DateTime (yyyy/MM/dd HH:mm:ss.fff) ID Log: Not available!]");
}
sbExInfo.AppendLine(" [Process information: " + GetCurrentProcessInformation() + "]");
sbExInfo.AppendLine(" [Thread information: " + GetCurrentThreadInformation() + "]");
sbExInfo.AppendLine(" [Log Assembly Name: " + (String.IsNullOrEmpty(LogAssemblyName) ? "Not available!" : LogAssemblyName) + "]");
if (ex != null)
{
LoggerFile.AddExceptionInfoToSB(sbExInfo, ex);
}
if (!String.IsNullOrEmpty(sTraceHeader) && !String.IsNullOrEmpty(sTrace))
{
sbExInfo.AppendLine(" [Trace header: " + sTraceHeader + "]");
sbExInfo.AppendLine(" [Trace: " + sTrace + "]");
}
else
{
sbExInfo.AppendLine(" [Trace: Not available!]");
}
}
private void LogIntoEventLog(string sMainMsg, string sDetailMsg)
{
string sMSG_LOG_XSRF_EVENTLOG = Properties.Resources.sEventLogMessageFormat;
string sSOURCE_MESSAGE_EVENTLOG = Properties.Resources.sSourceEventLog;
WriteEventLogEntry(string.Format(sMSG_LOG_XSRF_EVENTLOG, sMainMsg) + sDetailMsg, sSOURCE_MESSAGE_EVENTLOG);
}
private string GetCurrentThreadInformation()
{
string sInfo = null;
Thread curThread = Thread.CurrentThread;
lock (_lockObj)
{
sInfo = String.Format("Name (thread representation): {0} ({1})\n", curThread.Name, curThread.ToString()) +
String.Format(" Background: {0}\n", curThread.IsBackground) +
String.Format(" Thread Pool: {0}\n", curThread.IsThreadPoolThread) +
String.Format(" Thread ID: {0}\n", curThread.ManagedThreadId);
}
return sInfo;
}
private string GetCurrentProcessInformation()
{
string sInfo = null;
string sCommandLine = "Not available!";
Process curProcess = Process.GetCurrentProcess();
lock (_lockObj)
{
try
{
sCommandLine = GetCommandLine(curProcess);
}
catch (Win32Exception ex) when ((uint)ex.ErrorCode == 0x80004005)
{
// Intentionally empty - no security access to the process.
}
catch (InvalidOperationException)
{
// Intentionally empty - the process exited before getting details.
}
sInfo = String.Format("Name (Id): '{0}' ({1})\n", curProcess.ProcessName, curProcess.Id) +
String.Format(" Total Process Time: {0}\n", curProcess.TotalProcessorTime) +
String.Format(" Commande line: {0}\n", sCommandLine);
}
return sInfo;
}
private string GetCommandLine(Process process)
{
// Using all managed objects, but it does dip down into the WMI realm
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
using (ManagementObjectCollection objects = searcher.Get())
{
return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
}
}
private static void WriteEventLogEntry(string sMessage, string sSource)
{
// Create an instance of EventLog
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
// Check if the event source exists. If not create it.
if (!System.Diagnostics.EventLog.SourceExists(sSource))
{
System.Diagnostics.EventLog.CreateEventSource(sSource, "Application");
}
// Set the source name for writing log entries.
eventLog.Source = sSource;
// Create an event ID to add to the event log
int eventID = 8;
// Write an entry to the event log.
eventLog.WriteEntry(sMessage,
System.Diagnostics.EventLogEntryType.Error,
eventID);
// Close the Event Log
eventLog.Close();
}
}
}