Upravit

Sdílet prostřednictvím


EventLog.Delete Method

Definition

Removes a log resource.

Overloads

Delete(String, String)

Removes an event log from the specified computer.

Delete(String)

Removes an event log from the local computer.

Delete(String, String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

Removes an event log from the specified computer.

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

Parameters

logName
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

The name of the computer to delete the log from, or "." for the local computer.

Exceptions

logName is an empty string ("") or null.

-or-

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. A Windows error code is not available.

Examples

The following example deletes a log from the specified computer. The example determines the log from its source.

Note

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

Remarks

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.

Delete removes the log specified by logName from the computer specified by machineName. If you want to delete only the source registered to a log, call DeleteEventSource. If you only want to delete the log entries, call Clear. Delete and DeleteEventSource are static methods, so they can be called on the class itself. 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.

Note

Recreating an event log can be a difficult process. Avoid deleting any of the system-created event logs, such as the Application log.

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.

See also

Applies to

Delete(String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

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)

Parameters

logName
String

The name of the log to delete. Possible values include: Application, Security, System, and any custom event logs on the computer.

Exceptions

logName 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. A Windows error code is not available.

Examples

The following example deletes a log from the local computer. The example determines the log from its source.

Note

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 MySample1
{
    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

Remarks

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.

Delete removes the log specified by logName from the local computer. If you want to delete only the source registered to a log, call DeleteEventSource. If you only want to delete the log entries, call Clear. Delete and DeleteEventSource are static methods, so they can be called on the class itself. It is not necessary to create a new instance of EventLog to call either method.

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.

Note

Recreating an event log can be a difficult process. Avoid deleting any of the system-created event logs, such as the Application log.

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.

See also

Applies to