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.
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");
}
}
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed in the "Applies to" section.
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: