Microsoft KB Archive/918122

= 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 =

Article ID: 918122

Article Last Modified on 12/3/2007

-

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, &quot;.&quot;);

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



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the issue
 Start Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project types click Visual C#. Under Templates, click Console Application. Type a project name, and then click OK.  In the Code window, add the following line of code to the using statements: using System.Diagnostics; </li>  Add the following code example to the Main function: EventLogPermission eventLogPermission = new EventLogPermission(EventLogPermissionAccess.Write, &quot;.&quot;);

eventLogPermission.PermitOnly; EventLog.WriteEntry(&quot;Source&quot;, &quot;Message&quot;); </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start Without Debugging.

Notice that the application throws a security exception.</li></ol>

<div class="references_section">