BUG: You receive a security exception error message when you call the EventLog.WriteEntry method by using the EventLogPermissionAccess.Write access level in the .NET Framework 2.0 (918122)



The information in this article applies to:

  • Microsoft .NET Framework 2.0
  • Microsoft Visual C# 2005, Express Edition

SYMPTOMS

Consider the following scenario. In the Microsoft .NET Framework 2.0, you create an instance of the EventLogPermission class that has the EventLogPermissionAccess.Write access level. You call the EventLogPermission.PermitOnly method to restrict code access. Then you call the EventLog.WriteEntry method to write an entry to the event log. In this scenario, you receive a security exception error message that resembles the following:
Unhandled Exception: System.Security.SecurityException: Request for the permission of type 'System.Diagnostics.EventLogPermission, ...' failed.

WORKAROUND

To work around this issue, you must request the EventLogPermissionAccess.Administer access level before you call the EventLogPermission.PermitOnly method. After you specify this access level, you can call the EventLog.WriteEntry method and write an entry to the event log. The following code example demonstrates how to write an entry to the event log by using this workaround.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
using System;
using System.Diagnostics;

public class TestCase
{
	public static void Main()
	{
		EventLogPermission eventLogPermission = new EventLogPermission(EventLogPermissionAccess.Administer, ".");

		eventLogPermission.PermitOnly();
		EventLog.WriteEntry("Source", "Message");
	}
}

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the issue

  1. Start Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project types click Visual C#.
  4. Under Templates, click Console Application.
  5. Type a project name, and then click OK.
  6. In the Code window, add the following line of code to the using statements:
    using System.Diagnostics;
  7. Add the following code example to the Main function:
    EventLogPermission eventLogPermission = new EventLogPermission(EventLogPermissionAccess.Write, ".");
    
    eventLogPermission.PermitOnly();
    EventLog.WriteEntry("Source", "Message");
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start Without Debugging.

    Notice that the application throws a security exception.

REFERENCES

For more information about the CodeAccessPermission.PermitOnly method, visit the following Microsoft Developer Network (MSDN) Web site: For more information about the EventLog.WriteEntry method, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:5/4/2006
Keywords:kbpending kbcode kbtshoot kbbug KB918122 kbAudDeveloper