1 |
#Region "Microsoft.VisualBasic::66a9a4e8f01366932d9f43d0d0a107cf, Microsoft.VisualBasic.Core\ApplicationServices\Debugger\Logging\EventLog.vb"
|
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 |
#End Region
|
45 |
|
46 |
Imports System.ComponentModel
|
47 |
Imports System.Reflection
|
48 |
Imports System.Runtime.CompilerServices
|
49 |
Imports Microsoft.VisualBasic.Linq.Extensions
|
50 |
|
51 |
Namespace ApplicationServices.Debugging.Logging
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
<InstallerType("System.Diagnostics.EventLogInstaller, System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>
|
58 |
<MonitoringDescription("EventLogDesc")>
|
59 |
Public Class EventLog
|
60 |
|
61 |
Public ReadOnly Property Product As String
|
62 |
Public ReadOnly Property Services As String
|
63 |
Public ReadOnly Property Initialized As Boolean
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
<param name="Product">Source</param>
|
69 |
<param name="Services"></param>
|
70 |
Sub New(Services As String, Product As String)
|
71 |
Me.Product = Product
|
72 |
Me.Services = Services
|
73 |
Try
|
74 |
Me.Initialized = __install(Services, Product)
|
75 |
Catch ex As Exception
|
76 |
Me.Initialized = False
|
77 |
End Try
|
78 |
End Sub
|
79 |
|
80 |
Private Shared Function __install(Services As String, Product As String) As Boolean
|
81 |
If Not (System.Diagnostics.EventLog.SourceExists(Product, ".")) Then
|
82 |
Dim evscd As New EventSourceCreationData(Product, Services) With {
|
83 |
.MachineName = Environment.MachineName
|
84 |
}
|
85 |
Try
|
86 |
Call System.Diagnostics.EventLog.CreateEventSource(evscd)
|
87 |
Catch ex As Exception
|
88 |
Call ex.PrintException
|
89 |
Return False
|
90 |
End Try
|
91 |
End If
|
92 |
|
93 |
Return True
|
94 |
End Function
|
95 |
|
96 |
|
97 |
Writes a localized entry to the event log.
|
98 |
|
99 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
100 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
101 |
<returns></returns>
|
102 |
Public Function WriteEntry(message As String,
|
103 |
Optional EventType As EventLogEntryType = EventLogEntryType.Information,
|
104 |
Optional category As Integer = 1) As Boolean
|
105 |
Try
|
106 |
#If DEBUG Then
|
107 |
Call message.__DEBUG_ECHO
|
108 |
#End If
|
109 |
Using evLog As New System.Diagnostics.EventLog(Services, ".", Product)
|
110 |
Call evLog.WriteEvent(New EventInstance(10001 + category, category, EventType), {message})
|
111 |
End Using
|
112 |
Catch ex As Exception
|
113 |
Return False
|
114 |
End Try
|
115 |
|
116 |
Return True
|
117 |
End Function
|
118 |
|
119 |
|
120 |
Writes a localized entry to the event log.
|
121 |
|
122 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
123 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
124 |
<returns></returns>
|
125 |
Public Function WriteEntry(Message As IEnumerable(Of String),
|
126 |
Optional EventType As EventLogEntryType = EventLogEntryType.Information,
|
127 |
Optional category As Integer = 0) As Boolean
|
128 |
Try
|
129 |
#If DEBUG Then
|
130 |
Dim s As String = Message.JoinBy(vbCrLf)
|
131 |
Call s.__DEBUG_ECHO
|
132 |
#End If
|
133 |
Using evLog As New System.Diagnostics.EventLog(Services, ".", Product)
|
134 |
Dim data As Object() = Message.Select(Function(str) DirectCast(str, Object)).ToArray
|
135 |
Call evLog.WriteEvent(New EventInstance(10001 + category * 55, category, EventType), data)
|
136 |
End Using
|
137 |
Catch ex As Exception
|
138 |
|
139 |
Return False
|
140 |
End Try
|
141 |
|
142 |
Return True
|
143 |
End Function
|
144 |
|
145 |
|
146 |
Writes a localized entry to the event log.
|
147 |
|
148 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
149 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
150 |
<returns></returns>
|
151 |
Public Function WriteEntry(Message As System.Collections.Generic.IEnumerable(Of String),
|
152 |
Trace As String,
|
153 |
Optional EventType As System.Diagnostics.EventLogEntryType = EventLogEntryType.Information,
|
154 |
Optional category As Integer = 0) As Boolean
|
155 |
Message = Message.Join("USER_TRACE: " & Trace)
|
156 |
Return WriteEntry(Message, EventType, category)
|
157 |
End Function
|
158 |
|
159 |
|
160 |
Writes a localized entry to the event log.
|
161 |
|
162 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
163 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
164 |
<returns></returns>
|
165 |
Public Function WriteEntry(Message As System.Collections.Generic.IEnumerable(Of String),
|
166 |
Trace As MethodBase,
|
167 |
Optional EventType As System.Diagnostics.EventLogEntryType = EventLogEntryType.Information,
|
168 |
Optional category As Integer = 0) As Boolean
|
169 |
Return WriteEntry(Message, Trace.GetFullName, EventType, category)
|
170 |
End Function
|
171 |
|
172 |
|
173 |
Writes a localized entry to the event log.
|
174 |
|
175 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
176 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
177 |
<returns></returns>
|
178 |
Public Function WriteEntry(Message As String, Trace As String, Optional EventType As System.Diagnostics.EventLogEntryType = EventLogEntryType.Information, Optional category As Integer = 2) As Boolean
|
179 |
Return WriteEntry({NameOf(Message) & ": " & Message, NameOf(Trace) & ": " & Trace}, EventType, category)
|
180 |
End Function
|
181 |
|
182 |
|
183 |
Writes a localized entry to the event log.
|
184 |
|
185 |
<param name="EventType">An <see cref="System.Diagnostics.EventLogEntryType"/> value that indicates the event type.</param>
|
186 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
187 |
<returns></returns>
|
188 |
Public Function WriteEntry(Message As String,
|
189 |
Trace As MethodBase,
|
190 |
Optional EventType As System.Diagnostics.EventLogEntryType = EventLogEntryType.Information,
|
191 |
Optional category As Integer = 2) As Boolean
|
192 |
Return WriteEntry(Message, Trace.GetFullName, EventType, category)
|
193 |
End Function
|
194 |
|
195 |
|
196 |
Writes a localized entry to the event log.
|
197 |
|
198 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
199 |
<returns></returns>
|
200 |
<param name="Trace">可以不需要进行额外的处理,编译器会自动在这个参数填充调用栈的位置</param>
|
201 |
|
202 |
#If NET_40 = 0 Then
|
203 |
Public Function LogException(ex As Exception, <CallerMemberName> Optional Trace As String = "", Optional category As Integer = 3) As Boolean
|
204 |
#Else
|
205 |
Public Function LogException(ex As Exception, Optional Trace As String = "", Optional category As Integer = 3) As Boolean
|
206 |
#End If
|
207 |
Dim Data As String = ""
|
208 |
|
209 |
If Not ex.Data Is Nothing Then
|
210 |
Try
|
211 |
Data = String.Join(vbCrLf, (From obj In ex.Data.Keys Select obj.ToString & " ==> " & ex.Data(obj).ToString).ToArray)
|
212 |
Catch
|
213 |
End Try
|
214 |
End If
|
215 |
|
216 |
#If NET_40 = 0 Then
|
217 |
Dim MSG_DATA As New List(Of String) From {
|
218 |
"Exception: " & Scripting.InputHandler.ToString(ex.Message),
|
219 |
"InnerException: " & Scripting.InputHandler.ToString(ex.InnerException),
|
220 |
"HelpLink: " & Scripting.InputHandler.ToString(ex.HelpLink),
|
221 |
"Handle: " & Scripting.InputHandler.ToString(ex.HResult),
|
222 |
"Source: " & Scripting.InputHandler.ToString(ex.Source)
|
223 |
}
|
224 |
#Else
|
225 |
Dim MSG_DATA As New List(Of String) From {
|
226 |
"Exception: " & Scripting.InputHandler.ToString(ex.Message),
|
227 |
"InnerException: " & Scripting.InputHandler.ToString(ex.InnerException),
|
228 |
"HelpLink: " & Scripting.InputHandler.ToString(ex.HelpLink),
|
229 |
"Source: " & Scripting.InputHandler.ToString(ex.Source)
|
230 |
}
|
231 |
#End If
|
232 |
Dim Stacks As String() = Strings.Split(Scripting.InputHandler.ToString(ex.StackTrace), vbCrLf)
|
233 |
Dim exTrace As String = If(ex.TargetSite Is Nothing OrElse ex.TargetSite.DeclaringType Is Nothing, "null", ex.TargetSite.DeclaringType.FullName)
|
234 |
Call MSG_DATA.AddRange((From deepth As Integer In Stacks.Sequence Select $"stack__{Stacks.Length - deepth}: {Stacks(deepth)}").ToArray)
|
235 |
Call MSG_DATA.AddRange({"Data " & Data, $"Trace: {exTrace}:{Scripting.ToString(ex.TargetSite)}", "USER_TRACE: " & Trace})
|
236 |
Return WriteEntry(MSG_DATA, EventLogEntryType.Error, category)
|
237 |
End Function
|
238 |
|
239 |
|
240 |
Writes a localized entry to the event log.
|
241 |
|
242 |
<param name="category">A resource identifier that corresponds to a string defined in the category resource file of the event source, or zero to specify no category for the event.</param>
|
243 |
<returns></returns>
|
244 |
Public Function LogException(ex As Exception, Trace As MethodBase, Optional category As Integer = 3) As Boolean
|
245 |
Return LogException(ex, Trace.GetFullName, category)
|
246 |
End Function
|
247 |
|
248 |
|
249 |
$"{<see cref="Services"/>}//{<see cref="Product"/>}"
|
250 |
|
251 |
<returns></returns>
|
252 |
Public Overrides Function ToString() As String
|
253 |
Return $"{Services}//{Product}"
|
254 |
End Function
|
255 |
End Class
|
256 |
End Namespace
|