Microsoft Knowledge Base Email Alertz

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

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 918122 - Last Review: December 3, 2007 - Revision: 1.4

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

On This Page

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:
http://msdn2.microsoft.com/en-us/library/system.security.codeaccesspermission.permitonly(VS.80).aspx (http://msdn2.microsoft.com/en-us/library/system.security.codeaccesspermission.permitonly(VS.80).aspx)
For more information about the EventLog.WriteEntry method, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/6hse04t8(vs.80).aspx (http://msdn2.microsoft.com/en-us/library/6hse04t8(vs.80).aspx)

APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbpending kbcode kbtshoot kbbug KB918122
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate