EventLog.DeleteEventSource EventLog.DeleteEventSource EventLog.DeleteEventSource EventLog.DeleteEventSource Method

定義

將應用程式的事件來源登錄從事件記錄檔移除。Removes an application's event source registration from the event log.

多載

DeleteEventSource(String) DeleteEventSource(String) DeleteEventSource(String) DeleteEventSource(String)

將事件來源登錄從本機電腦的事件記錄檔移除。Removes the event source registration from the event log of the local computer.

DeleteEventSource(String, String) DeleteEventSource(String, String) DeleteEventSource(String, String) DeleteEventSource(String, String)

將應用程式的事件來源登錄從指定的電腦移除。Removes the application's event source registration from the specified computer.

DeleteEventSource(String) DeleteEventSource(String) DeleteEventSource(String) DeleteEventSource(String)

將事件來源登錄從本機電腦的事件記錄檔移除。Removes the event source registration from the event log of the local computer.

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

參數

source
String String String String

應用程式在事件記錄檔系統中登錄的名稱。The name by which the application is registered in the event log system.

例外狀況

本機電腦的登錄中沒有 source 參數。The source parameter does not exist in the registry of the local computer.

-或--or- 您沒有事件記錄檔登錄機碼的寫入權限。You do not have write access on the registry key for the event log.

範例

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

注意

一個以上的來源可能會寫入事件記錄檔。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.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
End Class 'MySample

備註

使用此方法可Source從本機電腦移除的註冊。Use this method to remove the registration of a Source from the local computer. DeleteEventSource存取本機電腦上的登錄,並移除應用程式的註冊,做為事件的有效來源。DeleteEventSource accesses the registry on the local computer and removes the registration of your application as a valid source of events.

如果您不再需要元件將專案寫入該記錄檔,您可以將其移除為有效的事件來源。You can remove your component as a valid source of events if you no longer need it to write entries to that log. 例如,如果您需要將元件從一個記錄檔變更為另一個,您可能會這麼做。For example, you might do this if you need to change your component from one log to another. 因為來源一次只能註冊到一個記錄檔,所以變更記錄檔會要求您移除目前的註冊。Because a source can only be registered to one log at a time, changing the log requires you to remove the current registration.

DeleteEventSource只移除註冊至記錄檔的來源。DeleteEventSource removes only the source registered to a log. 如果您想要移除記錄檔本身,請Delete呼叫。If you want to remove the log itself, call Delete. 如果您只想要刪除記錄檔專案,請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.

透過呼叫來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.

注意

如果來源已經對應到記錄檔,而且您將它重新對應到新的記錄檔,您必須重新開機電腦,變更才會生效。If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

安全性

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

另請參閱

DeleteEventSource(String, String) DeleteEventSource(String, String) DeleteEventSource(String, String) DeleteEventSource(String, String)

將應用程式的事件來源登錄從指定的電腦移除。Removes the application's event source registration from the specified computer.

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

參數

source
String String String String

應用程式在事件記錄檔系統中登錄的名稱。The name by which the application is registered in the event log system.

machineName
String String String String

要移除登錄的電腦名稱,或者表示本機電腦的 "."。The name of the computer to remove the registration from, or "." for the local computer.

例外狀況

machineName 參數無效。The machineName parameter is invalid.

-或--or- 指定電腦的登錄中沒有 source 參數。The source parameter does not exist in the registry of the specified computer.

-或--or- 您沒有事件記錄檔登錄機碼的寫入權限。You do not have write access on the registry key for the event log.

無法刪除 source,因為在登錄中,source 的父登錄機碼並未包含具有相同名稱的子機碼 (Subkey)。source cannot be deleted because in the registry, the parent registry key for source does not contain a subkey with the same name.

範例

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

注意

一個以上的來源可能會寫入事件記錄檔。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.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
End Class 'MySample

備註

使用此多載, Source從遠端電腦移除的註冊。Use this overload to remove the registration of a Source from a remote computer. DeleteEventSource存取所指定machineName電腦上的登錄,並移除應用程式的註冊,做為事件的有效來源。DeleteEventSource accesses the registry on the computer specified by machineName and removes the registration of your application as a valid source of events.

如果您不再需要元件將專案寫入該記錄檔,您可以將其移除為有效的事件來源。You can remove your component as a valid source of events if you no longer need it to write entries to that log. 例如,如果您需要將元件從一個記錄檔變更為另一個,您可能會這麼做。For example, you might do this if you need to change your component from one log to another. 因為來源一次只能註冊到一個記錄檔,所以變更記錄檔會要求您移除目前的註冊。Because a source can only be registered to one log at a time, changing the log requires you to remove the current registration.

DeleteEventSource只移除註冊至記錄檔的來源。DeleteEventSource removes only the source registered to a log. 如果您想要移除記錄檔本身,請Delete呼叫。If you want to remove the log itself, call Delete. 如果您只想要刪除記錄檔專案,請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.

透過呼叫來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.

注意

如果來源已經對應到記錄檔,而且您將它重新對應到新的記錄檔,您必須重新開機電腦,變更才會生效。If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

安全性

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

另請參閱

適用於