Bu adım adım makalede, Microsoft .NET Framework kullanarak, Windows Olay günlüklerini özelleştirme açıklamaktadır.
EventLog sınıf'ı kullanarak, Windows olay günlükleriyle etkileşim kurabilirsiniz.
EventLog sınıfı, şunları yapmak için kullanabilirsiniz:
- Mevcut günlüklerden</a1> makalesini okuyun.
- Girişler için olay günlüklerini yazın.
- Oluşturma veya olay kaynaklarını silme.
- Günlükleri Sil.
- Girdileri günlüğe kaydetmek için yanıt'ı tıklatın.
Bu makalede ayrıca bir olay kaynağı oluştururken, yeni günlükler oluşturmak açıklamaktadır.
Gereksinimler
Aşağıdaki listede, gereksinim duyulan donanım, yazılım, ağ altyapısı ve hizmet paketleri önerilmektedir:
- Microsoft .NET framework
- Microsoft Visual Basic .NET'i veya Microsoft Visual Basic 2005
Bu makalede, aşağıdaki konularda bilgi sahibi olduğunuz varsayılmaktadır:
- Microsoft Visual Basic .NET veya Microsoft Visual Basic 2005 sözdizimi
- Microsoft Visual Studio .NET veya Microsoft Visual Studio 2005 ortamı
- .NET Framework'de işleme hatası
Bir bilgisayarda varolan günlüklerin bulma
EventLog sınıfının paylaşılan
GetEventLogs yöntemini kullanarak, bir bilgisayarda varolan günlüklerin bulabilirsiniz. Yerel bilgisayardaki tüm olay günlüklerini
GetEventLogs yöntemi arar ve sonra listeyi içeren
EventLog nesnelerinin bir dizisi oluşturur. Aşağıdaki kod örneği, yerel bilgisayardaki günlükleri listesini alır ve sonra günlüklerinin adlarını, bir konsol penceresinde görüntüler:
Dim remoteEventLogs() As EventLog
'Gets logs on the local machine, give remote machine name to get the logs on the remote machine
remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)
Console.WriteLine("Number of logs on computer: " & remoteEventLogs.Length)
'Display the list of event logs
Dim log As EventLog
For Each log In remoteEventLogs
Console.WriteLine("Log: " & log.Log)
Next logOkuma ve gelen yerel ve uzak sistem günlükleri yazma
Günlükleri'ni okuyun
Olay günlüğünü okumak için <a0></a0>,
EventLog sınıfının
girişleri özelliğini kullanın.
EventLog sınıfının
girişleri özelliği, olay günlüğündeki tüm girişleri topluluğudur. Bu topluluk içinde yineleme yapmak ve belirtilen günlüğündeki tüm girişleri'ni okumak aşağıdaki kod örneği gösterilmektedir:
' Log type can be Application, Security, System or any other custom log
' Select the log type you want to read
Dim logtype As String = "Application"
' In the constructor of the eventlog, pass the log type and the computer name
' from which you want to read the logs
Dim evtLog As New EventLog(logtype, System.Environment.MachineName)
Dim lastlogtoshow As Integer = evtLog.Entries.Count
If lastlogtoshow <= 0 Then
Console.WriteLine("There are no event logs in the log : " & logtype)
Exit Sub
End If
' Read the last record in the specified log
Dim currentEntry As EventLogEntry
Dim i As Integer
' Show Last 2 entries. You can similarly write the log to a file.
For i = evtLog.Entries.Count - 1 To lastlogtoshow - 2 Step -1
currentEntry = evtLog.Entries(i)
Console.WriteLine("Event Id is : " & currentEntry.EventID)
Console.WriteLine("Entry type is : " & currentEntry.EntryType.ToString())
Console.WriteLine("Message is : " & currentEntry.Message & vbCrLf)
Next
evtLog.Close()Günlükleri yazma
Olay günlüğü yazmak için <a0></a0>,
EventLog sınıfının
WriteEntry yöntemini kullanın. Olay günlüğü başarıyla yazmak için <a0></a0>, uygulama günlüğü için yazma için yazma erişimi olduğundan emin olun. Olay günlüklerinde okuyup için gereken izinler hakkında daha fazla bilgi için aşağıdaki Microsoft Web sitesini ziyaret edin.
Güvenlik Ramification, olay günlükleri
http://msdn2.microsoft.com/en-us/library/4xz6w79h(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/4xz6w79h(vs.71).aspx)
Günlüğe bir girdi yazmadan önce
EventLog bileşeni örneğinde
Source özelliğini ayarlamanız gerekir. Bile?eniniz bir girdi yazdığında, sistem otomatik olarak belirttiğiniz kaynak bileşen yazma olay günlüğüne kaydedilir doğrular. Sistem daha sonra gerekirse
CreateEventSource çağırır. Olay günlüğü yazmak için <a0></a0>, günlük bulunduğu makine adını geçmelidir. Aşağıdaki kod örneği <a0>
ortam</a0> sınıfının
MachineName özelliği yerel makine adı belirler:
' Check if the source exists
If Not EventLog.SourceExists("MySystemSource", System.Environment.MachineName) Then
EventLog.CreateEventSource("MySystemSource", "System", System.Environment.MachineName)
End If
Dim evtLog As New EventLog("System", System.Environment.MachineName, "MySystemSource")
'writing to system log, in the similar way you can write to other
'logs for which you have appropriate permissions to write
evtLog.WriteEntry("warning is written to system log", EventLogEntryType.Warning, CInt(10001))
Console.WriteLine("Log written to the system log.")
evtLog.Close()Açık günlükleri
Olay günlüğü dolduğu zaman, yeni olay bilgilerini kaydetmeyi durdurur veya önceki girdilerinin üzerine başlar. Olay kaydı durdurursa, varolan girdilerin günlüğünü ve kayıt olayları yeniden başlatmaya izin. Olay günlük girdilerini temizlemek için <a0></a0>, günlük bulunduğu bilgisayar için yönetici izinlerinizin olması gerekir.
Clear yöntemi
EventLog bileşen örneği üzerinde çağırın.
Aşağıdaki örnek domonstrates nasıl bir günlüğü temizlemek kod:
' Create an EventLog instance and pass log name and MachineName on which the log resides
Dim evtLog As New EventLog("Security", System.Environment.MachineName)
evtLog.Clear()
evtLog.Close()Oluşturma ve silme özel günlükleri
Özel günlük oluşturma
CreateEventSource yöntemi, kendi özel bir olay işleyicisi oluşturmak için kullanın. Olay günlüğü oluşturmadan önce kullanmakta olduğunuz kaynak önceden var ve sonra
CreateEventSource call doğrulamak için
SourceExists yöntemini kullanın. Varolan bir olay günlüğünü oluşturma açmaya çalışırlarsa, bir
System.ArgumentException hata atılır.
Aşağıdaki kod örneği, özel bir günlük oluşturmak gösterilmiştir:
' Check if the log already exist
If Not EventLog.SourceExists("MyOldSource", System.Environment.MachineName) Then
' Creating a new log
EventLog.CreateEventSource("MyOldSource", "MyNewLog", System.Environment.MachineName)
Console.WriteLine("New event log created successfully.")
End IfÖzel günlük silme
Olay günlüğü silmek için <a0>
EventLog</a0> sınıfının <a2>
Sil</a2> yöntemini kullanın. Birden fazla kaynak için olay günlüğünü yazabilirsiniz. Bu nedenle, bir özel bir günlük silmeden önce günlüğe yazma başka hiçbir kaynağı olduğundan emin olun.
Aşağıdaki kod örneği, özel bir günlüğü silmek gösterilmiştir:
Dim logName As String = "MyNewLog"
If EventLog.SourceExists("MyOldSource", System.Environment.MachineName) Then
logName = EventLog.LogNameFromSourceName("MyOldSource", System.Environment.MachineName)
EventLog.DeleteEventSource("MyOldSource", System.Environment.MachineName)
EventLog.Delete(logName, System.Environment.MachineName)
Console.WriteLine(logName & " deleted.")
End IfOlay almak bildirimleri
Bir girdi, belirli bir günlüğe yazılır, bir olay bildirimi alabilir. Bunu yapmak için <a0></a0>,
EntryWritten olay işleyicisi
EventLog örneği için Ek Yardım düğmesini kullanın. Ayrıca
EnableRaisingEvents özelliğini
true olarak ayarlayın.
Aşağıdaki kod örneği, olay bildirimleri almak gösterilmiştir:
If Not EventLog1.SourceExists("MySource", System.Environment.MachineName) Then
EventLog1.CreateEventSource("MySource", "Application", System.Environment.MachineName)
Console.WriteLine("CreatingEventSource")
End If
'Enable EnableRaisingEvents to true
EventLog1.Log = "Application"
EventLog1.EnableRaisingEvents = True
EventLog1.WriteEntry("MySource", "EntryWritten event is fired", EventLogEntryType.Information)
End Sub girdisi, yerel bilgisayarda yazılır,
Not, yalnızca olay bildirimleri alabilir. Uzak bilgisayarlarda yazılmış girişler için bildirim alamazlar.
Kod tamamlamak listesi
Imports System.Diagnostics
Imports System.Security
Imports System.ComponentModel
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'The Windows Form Designer requires this call.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The Windows Form Designer requires the following procedure
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents EventLog1 As System.Diagnostics.EventLog
Friend WithEvents btnListLog As System.Windows.Forms.Button
Friend WithEvents btnReadLog As System.Windows.Forms.Button
Friend WithEvents btnWriteLog As System.Windows.Forms.Button
Friend WithEvents btnClearLog As System.Windows.Forms.Button
Friend WithEvents btnCreateLog As System.Windows.Forms.Button
Friend WithEvents btnDeleteLog As System.Windows.Forms.Button
Friend WithEvents btnRecNotice As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnReadLog = New System.Windows.Forms.Button()
Me.btnWriteLog = New System.Windows.Forms.Button()
Me.btnClearLog = New System.Windows.Forms.Button()
Me.btnCreateLog = New System.Windows.Forms.Button()
Me.btnDeleteLog = New System.Windows.Forms.Button()
Me.btnRecNotice = New System.Windows.Forms.Button()
Me.EventLog1 = New System.Diagnostics.EventLog()
Me.btnListLog = New System.Windows.Forms.Button()
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnReadLog
'
Me.btnReadLog.Location = New System.Drawing.Point(48, 54)
Me.btnReadLog.Name = "btnReadLog"
Me.btnReadLog.Size = New System.Drawing.Size(152, 24)
Me.btnReadLog.TabIndex = 0
Me.btnReadLog.Text = "Read Event Logs"
'
'btnWriteLog
'
Me.btnWriteLog.Location = New System.Drawing.Point(48, 86)
Me.btnWriteLog.Name = "btnWriteLog"
Me.btnWriteLog.Size = New System.Drawing.Size(152, 24)
Me.btnWriteLog.TabIndex = 1
Me.btnWriteLog.Text = "Write Event Logs"
'
'btnClearLog
'
Me.btnClearLog.Location = New System.Drawing.Point(48, 118)
Me.btnClearLog.Name = "btnClearLog"
Me.btnClearLog.Size = New System.Drawing.Size(152, 24)
Me.btnClearLog.TabIndex = 2
Me.btnClearLog.Text = "Clear Logs"
'
'btnCreateLog
'
Me.btnCreateLog.Location = New System.Drawing.Point(48, 150)
Me.btnCreateLog.Name = "btnCreateLog"
Me.btnCreateLog.Size = New System.Drawing.Size(152, 24)
Me.btnCreateLog.TabIndex = 3
Me.btnCreateLog.Text = "Create Custom Log"
'
'btnDeleteLog
'
Me.btnDeleteLog.Location = New System.Drawing.Point(48, 182)
Me.btnDeleteLog.Name = "btnDeleteLog"
Me.btnDeleteLog.Size = New System.Drawing.Size(152, 24)
Me.btnDeleteLog.TabIndex = 4
Me.btnDeleteLog.Text = "Delete Custom Log"
'
'btnRecNotice
'
Me.btnRecNotice.Location = New System.Drawing.Point(48, 214)
Me.btnRecNotice.Name = "btnRecNotice"
Me.btnRecNotice.Size = New System.Drawing.Size(152, 24)
Me.btnRecNotice.TabIndex = 5
Me.btnRecNotice.Text = "Receive Event Notifications"
'
'EventLog1
'
Me.EventLog1.EnableRaisingEvents = True
Me.EventLog1.Log = "Application"
Me.EventLog1.MachineName = System.Environment.MachineName
Me.EventLog1.SynchronizingObject = Me
'
'btnListLog
'
Me.btnListLog.Location = New System.Drawing.Point(48, 22)
Me.btnListLog.Name = "btnListLog"
Me.btnListLog.Size = New System.Drawing.Size(152, 24)
Me.btnListLog.TabIndex = 6
Me.btnListLog.Text = "List Event Logs"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(256, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnListLog, Me.btnRecNotice, Me.btnDeleteLog, Me.btnCreateLog, Me.btnClearLog, Me.btnWriteLog, Me.btnReadLog})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnReadLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadLog.Click
'logType can be Application, Security, System or any other Custom Log
Dim logType As String = "Application"
'In this case the EventLog constructor is passed a string variable for the log name and
'second argument mention the computer name from which you want to read the logs
'that you have appropriate permissions for
Dim ev As New EventLog(logType, System.Environment.MachineName)
Dim LastLogToShow As Integer = ev.Entries.Count
If LastLogToShow <= 0 Then
Console.WriteLine("No Event Logs in the Log :" & logType)
Exit Sub
End If
' read the last 2 records in the specified log
Dim i As Integer
For i = ev.Entries.Count - 1 To LastLogToShow - 2 Step -1
Dim CurrentEntry As EventLogEntry = ev.Entries(i)
Console.WriteLine("Event ID : " & CurrentEntry.EventID)
Console.WriteLine("Entry Type : " & CurrentEntry.EntryType.ToString())
Console.WriteLine("Message : " & CurrentEntry.Message & vbCrLf)
Next
ev.Close()
' Similarly, you can loop through all the entries in the log by using
' the entries collection, as shown in the following commented code.
' For Each entry In ev.Entries
' Next
End Sub
Private Sub btnWriteLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteLog.Click
' When writing to an event log, you must pass the machine name where
' the log resides. Here the MachineName Property of the Environment class
' is used to determine the name of the local machine. Assuming you have
' the appropriate permissions, it is also easy to write to event logs on
' other machines.
'Check if the Source exists
If Not EventLog.SourceExists("MySystemSource", System.Environment.MachineName) Then
EventLog.CreateEventSource("MySystemSource", "System", System.Environment.MachineName)
End If
Dim ev As New EventLog("System", System.Environment.MachineName, "MySystemSource")
'Writing to system log, in the similar way you can write to other
'logs that you have appropriate permissions to write to
ev.WriteEntry("Warning is written to system Log", EventLogEntryType.Warning, CInt(10001))
MessageBox.Show("Warning is written to System Log")
ev.Close()
End Sub
Private Sub btnClearLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLog.Click
' Create an EventLog instance and pass the log name and MachineName on which the log resides.
Dim ev As New EventLog("Security", System.Environment.MachineName)
ev.Clear()
ev.Close()
End Sub
Private Sub btnCreateLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateLog.Click
'Create the source, if it does not already exist.
If Not EventLog.SourceExists("MyOldSource", System.Environment.MachineName) Then
'Creating a new log
EventLog.CreateEventSource("MyOldSource", "MyNewLog", System.Environment.MachineName)
Console.WriteLine("CreatingEventSource")
End If
End Sub
Private Sub btnDeleteLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteLog.Click
Dim logName As String = "MyNewLog"
If EventLog.SourceExists("MyOldSource", System.Environment.MachineName) Then
logName = EventLog.LogNameFromSourceName("MyOldSource", System.Environment.MachineName)
EventLog.DeleteEventSource("MyOldSource", System.Environment.MachineName)
EventLog.Delete(logName, System.Environment.MachineName)
Console.WriteLine(logName & " deleted.")
End If
End Sub
Private Sub btnRecNotice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecNotice.Click
' Create the source if it does not already exist.
If Not EventLog1.SourceExists("MySource", System.Environment.MachineName) Then
EventLog1.CreateEventSource("MySource", "Application", System.Environment.MachineName)
Console.WriteLine("CreatingEventSource")
End If
'Enable EnableRaisingEvents to true
EventLog1.Log = "Application"
EventLog1.EnableRaisingEvents = True
EventLog1.WriteEntry("MySource", "EntryWritten event is fired", EventLogEntryType.Information)
End Sub
Private Sub EventLog1_EntryWritten(ByVal sender As Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs) Handles EventLog1.EntryWritten
If e.Entry.Source = "MySource" Then
Console.WriteLine("Entry written by my app. Message: " & e.Entry.Message)
End If
End Sub
Private Sub btnListLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListLog.Click
Dim remoteEventLogs() As EventLog
'Gets logs on the local machine, give remote machine name to get the logs on the remote machine
remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName)
Console.WriteLine("Number of logs on computer: " & remoteEventLogs.Length)
Dim log As EventLog
For Each log In remoteEventLogs
Console.WriteLine("Log: " & log.Log)
Next log
End Sub
End Class
Not Visual Basic 2005 kodunu değiştirmeniz gerekir. Windows Forms bir proje oluşturduğunuzda, varsayılan olarak, Visual Basic projesinin iki dosya oluşturur. Formun adı Form1 verilirse, formun temsil eden iki dosya Form1.vb ve Form1.Designer.vb olarak adlandırılır. Kodu, Form1.vb dosyasında yazın. Windows Forms Designer kod Form1.Designer.vb dosyaya yazar. Windows Forms Designer kısmi anahtar Form1 uygulamasının iki ayrı dosyaya bölmek için kullanır. Bu davranış, kodunuzu interspersed designer tarafından üretilen kod engeller.
Yeni Visual Basic 2005 dil geliştirmeleri hakkında daha fazla bilgi için aşağıdaki Microsoft Developer Network (MSDN) Web sitesini ziyaret edin:
Kısmi sınıfları ve Windows Forms Tasarımcısı hakkında daha fazla bilgi için aşağıdaki MSDN Web sitesini ziyaret edin:
Sonuçları doğrulayın.
Verfiy için sonuçları, aşağıdaki adımları izleyin:
- Visual Basic .NET veya Visual Basic 2005 kullanarak yeni bir Windows uygulaması oluşturun.
Varsayılan olarak, Form1.vb oluşturulur. - Kod, bu makalenin "Tam kod listesi" bölümünde kodda Form1.vb değiştirin.
- Hata Ayıkla) menüsünde, uygulamayı çalıştırmak için Başlat ' ı tıklatın.
- Eylemleri Form1.vb üzerinde gerçekleştirebilirsiniz.
- Sonuç doğrulamak için <a0></a0>, Server Explorer'ı açın. Bunu yapmak için <a0></a0>, Görünüm menüsünden Server Explorer ' ı tıklatın.
- Sunucular) düğümünü genişletin ve sonra Bilgisayar adınızı genişletin.
- Bilgisayar adınızı altında olay günlükleri ' ni genişletin.
Not Server Explorer <a1>Servers</a1> düğümünde, Visual Basic .NET akademik Sürüm'de kullanılamaz. Kullandığınız Windows Olay Görüntüleyicisi Uygulama sonuçlarını görüntülemek için. - Bu yordamdaki adımları doğru olarak gerçekleştirilen doğrulamak için Server Gezgini'ni kullanın.