????? ????? ????? ??????? ???????? Visual Basic .NET ?? Visual Basic 2005

?????? ????????? ?????? ?????????
???? ???????: 814564 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual C# .NET ?? ??? ???????? ???? 815314.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ?????? ??? ????? ????? ????? Windows ???????? Microsoft .NET Framework. ????? ??????? ?? ????? ????? Windows "???????? ??? ??? ???????. ????? ??????? ??? ??? ??????? ????? ??????? ???????:
  • ????? ?? ????? ??????.
  • ????? ??????? ????? ???????.
  • ????? ?? ??? ????? ???????.
  • ??? ?????.
  • ??????? ????? ???????.
???? ??? ??????? ????? ????? ????? ????? ????? ????? ????? ???? ???.


???????


???? ??????? ??????? ??????? ?????? ??? ????? ?????? ???????? ?????? ? ??? ??????? ????????:
  • ???? ??? Microsoft .NET
  • Microsoft Visual Basic .NET ?? Microsoft Visual Basic 2005

????? ??? ??????? ??? ??????? ??? ???????? ???????:
  • Microsoft Visual Basic .NET ?? Microsoft Visual Basic 2005 ??????
  • Microsoft Visual Studio .NET ?? ???? Microsoft Visual Studio 2005
  • ??? ?????? ?? .NET Framework

????? ?? ????? ?????? ?? ?????????


????? ????? ?? ????? ?????? ??? ???? ??????? ???????? ????? GetEventLogs ???????? ????? ??? ???????. ????? GetEventLogs ???? ?? ???? ????? ??????? ??? ????????? ?????? ??? ?? ???? ?????? ???? ?? ???????? ??? ??????? ???? ????? ??? ???????. ???? ???????? ???????? ??????? ????? ????? ????? ??? ????????? ?????? ??? ?? ???? ????? ??????? ?? ???? ???? ????: remoteEventLogs()
      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 log

??????? ???????? ????? ?? ?????? ??? "?" ?????? ??????


????? ?????


?????? ??? ??????? ??????? ????? ??????? ??? ??? ???????. ????? ??????? ??? ??? ??????? ?????? ?? ???? ????????? ?? ??? ???????. ???? ?????? ????????? ???????? ?????? ????? ????? ???? ??? ???????? ?????? ????? ???? ????????? ???????? ?? ????? ??????:
      ' 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()

????? ?????


?????? ??? ??????? "? ?????? ??????? WriteEntry ????? ??? ???????. ????? ??? ??????? ?????, ???? ?? ??????? ????? ?? ???? ?? ?????? ???? ??????? ???. ????? ?? ????????? ??? ???????? ???? ??? ?? ???? ???? ??????? ???????? ?? ????? ??????? ?? ?????? ???? Microsoft ?????? ??? ?????.

Ramification ?????? ?? ????? ???????
http://msdn2.microsoft.com/en-us/library/4xz6w79h(vs.71).aspx
??? ????? ??????? Source ??? ???? ???? "??? ??????? ????? ?? ??? ????? ??????? ??? ???. ????? ???? ?????? ???? ????? ?? ????? ? ?????? ???????? ????? ????? ???? ???? ????? ?? ??? ??????? ??????? ?????? ???. ????????? ?????? ?? CreateEventSource ??? ??? ?????. ?????? ??? ??????? ???? ??? ????? ??? ?????? ??? ???? ?????. ?? ???? ???????? ???????? ?????? ????? MachineName ??? ???? ???? ??? ?????? ??????:
      
  ' 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()

??? ?????


??? ?????? ??? ???????, ????? ?? ????? ??????? ????? ????? ?? ???? ?? ??????? ??? ????????? ???????. ??? ???? ????? ????? ????? ??? ????? ??????? ?? ?????? ??? ??? ????? ??????? ??? ????. ???? ??????? ??? ??????? "? ??? ?? ???? ???? ?????? ????? ???? ????????? ???? ???? ??? ?????. ??????? ??????? ??? ??? ???? ???? ??? ???????.

domonstrates ???? ???????? ???????? ??????? ????? ??? ???:
      ' 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()

????? ? ??? ????? ????

????? ????? ????

??????? ????? CreateEventSource ????? ????? ??????? ??????? ?????? ??. ??? ????? ??? ??????? ??????? ??????? SourceExists ???? ?? ?? ?????? ???? ??????? ?????? ???? ?? ??????? CreateEventSource. ??? ????? ????? ??? ??????? ?????? ??? ??? ??? System.ArgumentException.

???? ?????? ????????? ???????? ?????? ????? ????? ????? ????:
      ' 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

??? ??? ????

?????? ??????? ??? ????? ??? ??????? ??? ??? ??? ???????. ?? ????? ???? ?? ???? ??? ???????. ????? ??? ??? ??? ????? ???? ?? ????? ???? ??????? ??? ??? ?????.

???? ?????? ????????? ???????? ?????? ????? ??? ??? ????: logName
      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

???? ????? ???????

????? ???? ????? ??? ??? ????? ????? ??? ??? ????. ?????? ????? ???? ?????? ????? ??????? EntryWritten ????? ??? ???????. ?????? ?? ?????? ??????? EnableRaisingEvents ??? true.

???? ?????? ????????? ???????? ?????? ????? ???? ??????? ?????:
      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
?????? ??? ???? ??? ???? ??????? ????? ??? ??????? ?????? ??? ????????? ??????. ?? ???? ???? ??????? ????????? ???? ??? ??????? ??? ????? ????????? ???????.


????? ??? ?????

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

?????? ??? ????? ????????? ???????? ?? Visual Basic 2005. ???? ???????? ????? Visual Basic ????? ??????? ??? ????? ?????? ????? ????? Windows. ??? ?? ????? ??????? Form1, ??? ????? ??????? ???? ???? ??????? Form1.vb ? Form1.Designer.vb. ????? ????????? ???????? ?? ??? Form1.vb. ???? ????? Windows ?????? ???????? ???????? ?? ??? Form1.Designer.vb. ???? ????? Windows ?????? ?????? ???????? ??????? ?????? ????? Form1 ??? ????? ??????. ???? ??? ?????? ?? ????????? ???????? ???? ?? ??????? ???? ?? ??? interspersed ?? ????????? ???????? ?????? ??.

????? ?? ????????? ??? ??????? ????? 2005 Visual Basic ??????? ?? ?????? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx
????? ?? ????????? ??? ???? ????? "?" ???? ????? Windows ?? ?????? ???? MSDN ??????:
http://msdn2.microsoft.com/en-us/library/ms171843.aspx

???? ?? ???????

verfiy ??????? ? ???? ??????? ???????:
  1. ????? ????? Windows ???? ???????? Visual Basic .NET ?? 2005 Visual Basic.

    ?????????? ??? ????? Form1.vb.
  2. ??????? ????? ?? Form1.vb ????????? ???????? ???????? ?? ??? "????? ??? ??????" ?? ??? ???????.
  3. ?? ??????? ????? ? ???? ??? "????" ?????? ???????.
  4. ????? ??????? ??? Form1.vb.
  5. ?????? ?? ??????? ??? "?????? ??????". ?????? ????? ???? ??? "?????? ??????" ?? ??????? ???.
  6. ?? ?????? ???? ?????? ?? ?? ?????? ??? ????????? ????? ??.
  7. ??? ????? ????????? ? ?? ?????? ????? ???????.

    ?????? ???? ?????? Explorer ?????? ??? ????? ?? Visual Basic .NET ???????. ????? ??????? Windows Event Viewer ???? ????? ??????? ????? ??.
  8. ??????? "?????? ??????" ?????? ?? ?? ??? ????? ???? ??????? ?? ??? ??????? ???? ????.

?????

????? ?? ?????????? ?? ?????? ???? Microsoft ?????? ??? ?????:

??? ??? ???????
http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog(vs.71).aspx

???????

???? ???????: 814564 - ????? ??? ??????: 04/?? ??????/1428 - ??????: 2.7
????? ???
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
????? ??????: 
kbmt kbvs2005swept kbvs2005applies kbeventservice kbnetwork kbmanaged kbprogramming kbeventlog kbhowtomaster KB814564 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????814564

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com