You receive the "Requested registry access is not allowed" error message when you try to create a custom event log

Article translations Article translations
Article ID: 842795 - View products that this article applies to.
Expand all | Collapse all

On This Page

SYMPTOMS

If you log on to a computer as a regular user, and if you try to use Microsoft Visual Studio .NET to create a custom event log to register events, you may receive the following error message:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Additional information: Requested registry access is not allowed.

CAUSE

This problem occurs because the user account that you used to log on does not have sufficient permissions.

The first time that you call the EventLog.CreateEventSource() method to create a custom event log, the custom event log entry is created under the following registry subkey:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
To create this subkey entry, you must have permission to write. However, the regular user account does not have permission to write. Therefore, you receive the error message that is mentioned in the "Symptoms" section.

WORKAROUND

Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:
322756 How to back up and restore the registry in Windows

To work around this problem, use one of the following methods:
  • Grant permission to create a custom event log
  • Install the custom event log as an administrator

Grant permission to create a custom event log

  1. Log on to the computer as an administrator.
  2. Click Start, click Run, type regedit in the Open box, and then click OK. The Registry Editor window appears.
  3. Locate the following registry subkey:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
  4. Right-click Eventlog, and then click Permissions. The Permissions for Eventlog dialog box appears.
  5. Click Advanced. The Advanced Security Settings for Eventlog dialog box appears.
  6. In the Name column, double-click the Users group. The Permission Entry for Eventlog dialog box appears.
  7. Select the Set Value check box, select the Create Subkey check box, and then click OK.
  8. Quit Registry Editor, and then log off from the administrator account.
  9. Log on to the computer as a regular user.
  10. Try to create a custom event log by using Visual Studio .NET, and then try to write to this event log by using an application that is built on Visual Studio .NET.

Install the custom event log as an administrator

  1. Log on to the computer as an administrator.
  2. Use the CreateEventSource method in Visual Studio .NET to create a custom event log and an event log source.
  3. Log off from the administrator account.
  4. Log on to the computer as a regular user.
  5. Run the application to write the entry to the custom event log.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a regular user account:
    1. Log on to your computer as an administrator.
    2. Right-click My Computer, and then click Manage. The Computer Management window appears.
    3. Under System Tools, expand Local Users and Groups.
    4. Right-click Users, and then click New User. The New User dialog box appears.
    5. Type the name of the new account in the User name box, and then type a single password in both the Password box and the Confirm password box.
    6. Clear the User must change password at next logon check box, select the Password never expires check box, and then click Create.
    7. Click Close to close the New User dialog box.
  2. Add the user to the Debugger User group:
    1. Under Local Users and Groups, click Users.
    2. In the Name column in the right pane of the Computer Management window, right-click the name of the new account, and then click Properties. The properties dialog box of the new account appears.
    3. Click the Member Of tab, and then click Add. The Select Groups dialog box appears.
    4. Click Advanced. The Select Groups dialog box appears.
    5. Click Find Now. The available groups appear in the Select Groups dialog box.
    6. In the Name (RDN) column, click Debugger Users, and then click OK.
    7. In the Selected Groups dialog box, click OK.
    8. In the properties dialog box of the new account, click OK.
    9. Log off from the administrator account.
  3. Create a Windows application:
    1. Log on to the computer by using the regular user account that you created in "Step 1 - Create a regular user account".
    2. Start Visual Studio .NET.
    3. On the File menu, point to New, and then click Project. The New Project dialog box appears.
    4. Under Project Types, click Visual C# Projects.
    5. Under Templates, click Windows Application.
    6. In the Name box, type the name of a project, and then click OK. By default, the Form1 form appears.
    7. Right-click the Form1 form, and then click View Code. The Form1.cs file appears.
    8. Replace the existing code with the following code:
      using System;
      using System.Drawing;
      using System.Collections;
      using System.ComponentModel;
      using System.Windows.Forms;
      using System.Data;
      using System.Diagnostics;
      
      namespace WindowsApplication1
      {
      	/// <summary>
      	/// Summary description for Form1.
      	/// </summary>
      	public class Form1 : System.Windows.Forms.Form
      	{
      		private System.Diagnostics.EventLog eventLog1;
      		private System.Windows.Forms.Button button1;
      		/// <summary>
      		/// Required designer variable.
      		/// </summary>
      		private System.ComponentModel.Container components = null;
      
      	
      
      
      		public Form1()
      		{
      			//
      			// Required for Windows Form Designer support
      			//
      			InitializeComponent();
      
      			if(!EventLog.SourceExists("Source1"))
      			{
      				EventLog.CreateEventSource("Source1","MyLog1");
      			}
      			eventLog1.Source = "Source1";
      			eventLog1.Log = "MyLog1";
      			//
      			// TODO: Add any constructor code after InitializeComponent call
      			//
      		}
      		
      		
      		
      		
      		/// <summary>
      		/// Clean up any resources being used.
      		/// </summary>
      		protected override void Dispose( bool disposing )
      		{
      			if( disposing )
      			{
      				if (components != null) 
      				{
      					components.Dispose();
      				}
      			}
      			base.Dispose( disposing );
      		}
      
      		#region Windows Form Designer generated code
      		/// <summary>
      		/// Required method for Designer support - do not modify
      		/// the contents of this method with the code editor.
      		/// </summary>
      		private void InitializeComponent()
      		{
      			this.eventLog1 = new System.Diagnostics.EventLog();
      			this.button1 = new System.Windows.Forms.Button();
      			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
      			this.SuspendLayout();
      			// 
      			// eventLog1
      			// 
      			this.eventLog1.SynchronizingObject = this;
      			// 
      			// button1
      			// 
      			this.button1.Location = new System.Drawing.Point(104, 104);
      			this.button1.Name = "button1";
      			this.button1.TabIndex = 0;
      			this.button1.Text = "button1";
      			this.button1.Click += new System.EventHandler(this.button1_Click);
      			// 
      			// Form1
      			// 
      			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      			this.ClientSize = new System.Drawing.Size(292, 273);
      			this.Controls.Add(this.button1);
      			this.Name = "Form1";
      			this.Text = "Form1";
      			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
      			this.ResumeLayout(false);
      
      		}
      		#endregion
      
      		/// <summary>
      		/// The main entry point for the application.
      		/// </summary>
      		[STAThread]
      		static void Main() 
      		{
      			Application.Run(new Form1());
      		}
      		
      		private void button1_Click(object sender, System.EventArgs e)
      		{
      			eventLog1.WriteEntry("The Button1 Is Clicked");
      		}
      	}
      }
      
    9. On the File menu, click Save Form1.cs to save the file.
  4. Run the project:
    1. On the Build menu, click Build Solution.
    2. Run the project. You may receive the error message that is mentioned in the "Symptoms" section.
Note The security for the permission to write to the event log registry subkey is enforced at the Event Logging API level in the operating system. By default, the custom event log permissions are same as the application event log permissions.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Creating and removing custom event logs
http://msdn2.microsoft.com/en-us/library/49dwckkz(vs.71).aspx

Writing entries to event logs
http://msdn2.microsoft.com/en-us/library/k00ce235(vs.71).aspx

Adding your application as a source of event log entries
http://msdn2.microsoft.com/en-us/library/xz73e171(vs.71).aspx

Event logging security
http://msdn2.microsoft.com/en-us/library/aa363658.aspx
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
329291 "Requested registry access is not allowed" error message when ASP.NET application tries to write new EventSource in the EventLog

Properties

Article ID: 842795 - Last Review: March 29, 2007 - Revision: 3.3
APPLIES TO
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
Keywords: 
kbtshoot kberrmsg kbregistry kbpermissions kbeventlog kbaccounts kbprb KB842795

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com