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



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0


Important This article contains information about how to modify the registry. Make sure to back up the registry before you modify it. Make sure that you know how to restore the registry if a problem occurs. For more information about how to back up, restore, and modify the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows registry

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

Warning Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.
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: 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


Modification Type:MajorLast Reviewed:8/31/2006
Keywords:kbtshoot kberrmsg kbRegistry kbpermissions kbEventLog kbaccounts kbprb KB842795 kbAudDeveloper