تعاملنا فى الأمثلة مع سجل الأحداث وأظهرنا محتوياته وتحكمنا فى بعضاً منها
لكن حان الوقت لنقوم ببناء سجل لنا ونقوم بتسجيل احداث تطبيق خاص بنا
يلزم لذلك أن نكون سجل أحداث ونعطيه اسم وأيضاً مصدر تسجيل الأحداث
-----------------------------------------------
ضف هذا الكلاس لمشروعك
كود :
Class EventLogs
Private EvLog As EventLog
Sub New(ByVal LogSource As String, ByVal LogName As String)
If EvLog Is Nothing Then
If Not EventLog.SourceExists(LogSource) Then
EventLog.CreateEventSource(LogSource, LogName)
End If
EvLog = New EventLog(LogName)
EvLog.Source = LogSource
End If
End Sub
Public Sub LogEvent(ByVal Message As String, _
ByVal EventType As EventLogEntryType)
EvLog.WriteEntry(Message, EventType)
End Sub
End Class
نشرحه
الكلاس اسمه EventLogs
عرفنا متغير من الفئة EventLog باسم EvLog
كود :
Private EvLog As EventLog
عرفنا اجرائية ووظفتها أننا نمرر لها معاملين
الأول مصدر تسجيل السجل
الثاني اسم السجل
بحيث يتم تكوين السجل فى حالة عدم وجوده
كود :
Sub New(ByVal LogSource As String, ByVal LogName As String)
If EvLog Is Nothing Then
If Not EventLog.SourceExists(LogSource) Then
EventLog.CreateEventSource(LogSource, LogName)
End If
EvLog = New EventLog(LogName)
EvLog.Source = LogSource
End If
End Sub
الاجرائية
كود :
Public Sub LogEvent(ByVal Message As String, _
ByVal EventType As EventLogEntryType)
EvLog.WriteEntry(Message, EventType)
End Sub
المسئولة عن تسجيل الأحداث
ونرسل لها الاسم والنوع
قم فى وضع هذا الكود كمتغير عام فى أول الفورم
كود :
Private Evl As New EventLogs("From BADRMEDIA Event Log", "BADRMEDIA Application")
اكتب قى حدث زر الأمر
كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim x As Integer = 50
Dim y As Integer = 0
Dim z As Integer = x / y
Catch ex As Exception
Evl.LogEvent(ex.Message & ControlChars.CrLf, EventLogEntryType.Error)
End Try
End Sub
هنا افتعلنا خطأ وهو القسمة على صفر
ونقوم بارساله لسجل الأحداث الخاص بنا
اذهب إلي
نافذة Event Viewer الموجودة فى داخل Control Panel
ستجد سجل انشا باسم BADRMEDIA Application
وبه الخطأ مسجل
الآن يمكن تسجيل ومراقبة برنامجك