EventLog.Delete EventLog.Delete EventLog.Delete EventLog.Delete Method

定義

移除記錄檔資源。Removes a log resource.

多載

Delete(String, String) Delete(String, String) Delete(String, String) Delete(String, String)

將事件記錄檔從指定的電腦移除。Removes an event log from the specified computer.

Delete(String) Delete(String) Delete(String) Delete(String)

將事件記錄檔從本機電腦移除。Removes an event log from the local computer.

Delete(String, String) Delete(String, String) Delete(String, String) Delete(String, String)

將事件記錄檔從指定的電腦移除。Removes an event log from the specified computer.

public:
 static void Delete(System::String ^ logName, System::String ^ machineName);
[System.MonoNotSupported("remote machine is not supported")]
public static void Delete (string logName, string machineName);
static member Delete : string * string -> unit
Public Shared Sub Delete (logName As String, machineName As String)

參數

logName
String String String String

要刪除的記錄檔名稱。The name of the log to delete. 可能的值包括:應用程式、安全性、系統和指定電腦上的任何自訂事件記錄檔。Possible values include: Application, Security, System, and any custom event logs on the specified computer.

machineName
String String String String

要刪除記錄檔的電腦的名稱,或者表示本機電腦的 "."。The name of the computer to delete the log from, or "." for the local computer.

例外狀況

logName 為空字串 ("") 或 nulllogName is an empty string ("") or null.

-或--or- machineName 不是有效的電腦名稱。machineName is not a valid computer name.

無法在目標電腦上開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened on the specified computer.

-或--or-

計數器不在指定的電腦上。The log does not exist on the specified computer.

事件記錄檔未成功清除。The event log was not cleared successfully.

-或--or-

無法開啟記錄檔。The log cannot be opened. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會從指定的電腦刪除記錄檔。The following example deletes a log from the specified computer. 此範例會從來源判斷記錄檔。The example determines the log from its source.

注意

一個以上的來源可能會寫入事件記錄檔。More than one source might write to an event log. 在刪除自訂記錄檔之前, 請確定沒有其他來源寫入該記錄檔。Before deleting a custom log, make sure there are no other sources writing to that log.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   String^ logName;
   if ( EventLog::SourceExists( "MySource", "MyMachine") )
   {
      
      // Find the log associated with this source.    
      logName = EventLog::LogNameFromSourceName( "MySource", "MyMachine" );
	  // Make sure the source is in the log we believe it to be in
      if (logName != "MyLog")
		  return -1;
      // Delete the source and the log.
      EventLog::DeleteEventSource( "MySource", "MyMachine" );
      EventLog::Delete( logName, "MyMachine" );
      Console::WriteLine( "{0} deleted.", logName );
   }
   else
        {
            // Create the event source to make next try successful.
			EventSourceCreationData^ mySourceData = gcnew EventSourceCreationData("MySource", "MyLog");
            mySourceData->MachineName = "MyMachine";
            EventLog::CreateEventSource(mySourceData);
        }
}

using System;
using System.Diagnostics;
using System.Threading;

class MySample
{

    public static void Main()
    {

        string logName;

        if (EventLog.SourceExists("MySource", "MyMachine"))
        {
            // Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MySource", "MyMachine");
            // Make sure the source is in the log we believe it to be in.
            if (logName != "MyLog")
                return;
            // Delete the source and the log.
            EventLog.DeleteEventSource("MySource", "MyMachine");
            EventLog.Delete(logName, "MyMachine");

            Console.WriteLine(logName + " deleted.");
        }
        else
        {
            // Create the event source to make next try successful.
            EventSourceCreationData mySourceData = new EventSourceCreationData("MySource", "MyLog");
            mySourceData.MachineName = "MyMachine";
            EventLog.CreateEventSource(mySourceData);
        }
    }
}

Option Explicit On
Option Strict On

Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        Dim logName As String

        If EventLog.SourceExists("MySource", "MyMachine") Then
            ' Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MySource", "MyMachine")
            ' Make sure the source is in the log we believe it to be in
            If (logName <> "MyLog") Then
                Return
            End If
            ' Delete the source and the log.
            EventLog.DeleteEventSource("MySource", "MyMachine")
            EventLog.Delete(logName, "MyMachine")

            Console.WriteLine((logName & " deleted."))
        Else
            ' Create the event source to make next try successful.
            Dim mySourceData As New EventSourceCreationData("MySource", "MyLog")
            mySourceData.MachineName = "MyMachine"
            EventLog.CreateEventSource(mySourceData)
        End If
    End Sub 'Main
End Class 'MySample

備註

當您想要刪除的記錄檔位於遠端電腦上時, 請使用此方法。Use this method when the log you want to delete is on a remote computer. 您可以刪除電腦上的任何記錄檔, 前提是您具有適當的登入權利。You can delete any log on the computer, provided you have the appropriate registry permissions.

DeletelogName從指定的電腦中移除指定machineName的記錄檔。Delete removes the log specified by logName from the computer specified by machineName. 如果您只想要刪除已登錄至記錄的來源, 請DeleteEventSource呼叫。If you want to delete only the source registered to a log, call DeleteEventSource. 如果您只想要刪除記錄檔專案, 請Clear呼叫。If you only want to delete the log entries, call Clear. DeleteDeleteEventSourcestatic方法, 因此可以在類別本身上呼叫。Delete and DeleteEventSource are static methods, so they can be called on the class itself. 您不需要建立的EventLog實例, 即可呼叫任一種方法。It is not necessary to create an instance of EventLog to call either method.

這個方法會先刪除保存記錄檔內容的檔案。This method first deletes the file that holds the contents of the log. 然後它會存取登錄, 並移除所有為該記錄檔註冊的事件來源。It then accesses the registry and removes all the event sources registered for that log. 如果您稍後再重新建立記錄檔, 您應該再次註冊事件來源 (如果要重複使用的話)。If you recreate the log at a later point, you should register the event sources again, if they are to be reused. 如果您未指定記錄檔名稱, 而未註冊事件來源和其他使用者寫入事件來源, 則會在應用程式事件記錄檔中建立事件來源。If you do not register the event sources and other users write to an event source without specifying a log name, the event source will be created in the Application event log. 因此, 先前能夠將專案寫入您所刪除和重新建立之記錄檔的應用程式將會改為寫入應用程式記錄檔, 因為它現在包含事件來源。Therefore, applications that previously were able to write entries to the log you deleted and recreated will write to the Application log instead, because it now contains the event source.

注意

重建事件記錄檔可能會是一個很棘手的程式。Recreating an event log can be a difficult process. 避免刪除任何系統建立的事件記錄檔, 例如應用程式記錄檔。Avoid deleting any of the system-created event logs, such as the Application log.

透過呼叫來Delete刪除記錄, 會自動刪除已登錄至該記錄檔的來源。Deleting a log through a call to Delete automatically deletes the sources registered to that log. 這可能會讓其他使用該記錄檔的應用程式無法執行。This can make other applications using that log inoperative.

安全性

EventLogPermission
用於管理電腦上的事件記錄檔資訊。for administering event log information on the computer. 相關聯的列舉:AdministerAssociated enumeration: Administer

另請參閱

Delete(String) Delete(String) Delete(String) Delete(String)

將事件記錄檔從本機電腦移除。Removes an event log from the local computer.

public:
 static void Delete(System::String ^ logName);
public static void Delete (string logName);
static member Delete : string -> unit
Public Shared Sub Delete (logName As String)

參數

logName
String String String String

要刪除的記錄檔名稱。The name of the log to delete. 可能的值包括:應用程式、安全性、系統和電腦上的任何自訂事件記錄檔。Possible values include: Application, Security, System, and any custom event logs on the computer.

例外狀況

logName 為空字串 ("") 或 nulllogName is an empty string ("") or null.

無法在目標電腦上開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened on the local computer.

-或--or-

計數器不在指定的電腦上。The log does not exist on the local computer.

事件記錄檔未成功清除。The event log was not cleared successfully.

-或--or-

無法開啟記錄檔。The log cannot be opened. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會從本機電腦刪除記錄檔。The following example deletes a log from the local computer. 此範例會從來源判斷記錄檔。The example determines the log from its source.

注意

一個以上的來源可能會寫入事件記錄檔。More than one source might write to an event log. 在刪除自訂記錄檔之前, 請確定沒有其他來源寫入該記錄檔。Before deleting a custom log, make sure there are no other sources writing to that log.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   String^ logName;
   if ( EventLog::SourceExists( "MySource" ) )
   {
      
      // Find the log associated with this source.    
      logName = EventLog::LogNameFromSourceName( "MySource", "." );
	  // Make sure the source is in the log we believe it to be in
      if (logName != "MyLog")
		  return -1;
      // Delete the source and the log.
      EventLog::DeleteEventSource( "MySource" );
      EventLog::Delete( logName );
      Console::WriteLine( "{0} deleted.", logName );
   }
   else
        {
            // Create the event source to make next try successful.
            EventLog::CreateEventSource("MySource", "MyLog");
        }
}

using System;
using System.Diagnostics;
using System.Threading;

class MySample
{

    public static void Main()
    {

        string logName;

        if (EventLog.SourceExists("MySource"))
        {
            // Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MySource", ".");
            // Make sure the source is in the log we believe it to be in.
            if (logName != "MyLog")
                return;
            // Delete the source and the log.
            EventLog.DeleteEventSource("MySource");
            EventLog.Delete(logName);

            Console.WriteLine(logName + " deleted.");
        }
        else
        {
            // Create the event source to make next try successful.
            EventLog.CreateEventSource("MySource", "MyLog");
        }
    }
}

Option Explicit On
Option Strict On

Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        Dim logName As String

        If EventLog.SourceExists("MySource") Then
            ' Find the log associated with this source.    
            logName = EventLog.LogNameFromSourceName("MySource", ".")
            ' Make sure the source is in the log we believe it to be in
            If (logName <> "MyLog") Then
                Return
            End If
            ' Delete the source and the log.
            EventLog.DeleteEventSource("MySource")
            EventLog.Delete(logName)

            Console.WriteLine((logName & " deleted."))
        Else
            ' Create the event source to make next try successful.
            EventLog.CreateEventSource("MySource", "MyLog")
        End If
    End Sub 'Main
End Class 'MySample

備註

當您想要刪除的記錄檔在本機電腦上時, 請使用這個方法。Use this method when the log you want to delete is on the local computer. 您可以刪除電腦上的任何記錄檔, 前提是您具有適當的登入權利。You can delete any log on the computer, provided you have the appropriate registry permissions.

DeletelogName從本機電腦移除指定的記錄檔。Delete removes the log specified by logName from the local computer. 如果您只想要刪除已登錄至記錄的來源, 請DeleteEventSource呼叫。If you want to delete only the source registered to a log, call DeleteEventSource. 如果您只想要刪除記錄檔專案, 請Clear呼叫。If you only want to delete the log entries, call Clear. DeleteDeleteEventSourcestatic方法, 因此可以在類別本身上呼叫。Delete and DeleteEventSource are static methods, so they can be called on the class itself. 您不需要建立的新實例EventLog來呼叫任一種方法。It is not necessary to create a new instance of EventLog to call either method.

Delete方法會先刪除保存記錄檔內容的檔案。The Delete method first deletes the file that holds the contents of the log. 然後它會存取登錄, 並移除所有為該記錄檔註冊的事件來源。It then accesses the registry and removes all the event sources registered for that log. 如果您稍後再重新建立記錄檔, 您應該再次註冊事件來源 (如果要重複使用的話)。If you recreate the log at a later point, you should register the event sources again, if they are to be reused. 如果您未指定記錄檔名稱, 而未註冊事件來源和其他使用者寫入事件來源, 則會在應用程式事件記錄檔中建立事件來源。If you do not register the event sources and other users write to an event source without specifying a log name, the event source will be created in the Application event log. 因此, 先前能夠將專案寫入您所刪除和重新建立之記錄檔的應用程式將會改為寫入應用程式記錄檔, 因為它現在包含事件來源。Therefore, applications that previously were able to write entries to the log you deleted and recreated will write to the Application log instead, because it now contains the event source.

注意

重建事件記錄檔可能會是一個很棘手的程式。Recreating an event log can be a difficult process. 避免刪除任何系統建立的事件記錄檔, 例如應用程式記錄檔。Avoid deleting any of the system-created event logs, such as the Application log.

透過呼叫來Delete刪除記錄, 會自動刪除已登錄至該記錄檔的來源。Deleting a log through a call to Delete automatically deletes the sources registered to that log. 這可能會讓其他使用該記錄檔的應用程式無法執行。This can make other applications using that log inoperative.

安全性

EventLogPermission
用於管理電腦上的事件記錄檔資訊。for administering event log information on the computer. 相關聯的列舉:AdministerAssociated enumeration: Administer

另請參閱

適用於