Microsoft KB Archive/327890

= BUG: Behavior of EventLog.Delete is Not Consistent with EventLog.Exists for Log Names Longer than 8 Characters =

Article ID: 327890

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1

-



This article was previously published under Q327890



SYMPTOMS
The behavior of the EventLog.Delete method is not consistent with the behavior of the EventLog.Exists method for log names that are longer than 8 characters.



CAUSE
The EventLog.Exists method uses only the first 8 characters of the passed string to determine whether a log exists. The EventLog.Delete method uses all of the passed string to identify the log to be deleted. This causes an inconsistency between the two methods.

For example, a user may want to verify whether a log exists and then delete it, as shown in the following code: if (EventLog.Exists(&quot;MyProgramLog1&quot;)) {   EventLog.Delete(&quot;MyProgramLog1&quot;); } The problem is that EventLog.Exists returns TRUE if any log beginning with MyProgra (8 characters) exists. An example scenario occurs as follows:
 * MyProgramLog2 (longer than 8 characters) exists.
 * MyProgramLog1 does not exist.
 * When EventLog.Delete is called, it determines whether the registry contains an entry named MyProgramLog1.
 * If EventLog.Delete does not find MyProgramLog1, an exception is thrown.



RESOLUTION
To work around this problem, instead of relying on the EventLog.Exists method to determine whether a log exists, an application confirms the existence of the following registry key

where  is the name of the event log that you want to verify.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The following C# sample code demonstrates how to work around this problem by determining whether the registry contains an event log. If found, the EventLog.Delete method deletes the log. using System; using System.Diagnostics; using Microsoft.Win32;

class MyClass {  private const string LOG_NAME = &quot;MyProgramLog1&quot;; private const string REG_PATH = &quot;SYSTEM\\CurrentControlSet\\Services\\Eventlog\\&quot;;

public static void Main(string[] args) {        // Determine whether the log exists. RegistryKey eventLogKey = Registry.LocalMachine.OpenSubKey(REG_PATH + LOG_NAME); if (eventLogKey == null) {        Console.WriteLine (&quot;Log &quot; + LOG_NAME + &quot; not found.&quot;); return; }

// Try to delete the log you just found. try {        EventLog.Delete(LOG_NAME); Console.WriteLine (&quot;Log &quot; + LOG_NAME + &quot; deleted.&quot;); }     catch (Exception e)      { Console.WriteLine (&quot;Unable to delete log &quot; + LOG_NAME + &quot;.\n&quot;              + e.ToString); }  } }

Keywords: kbbug kbnofix kbkernbase KB327890

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.