Microsoft KB Archive/842795

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

Article ID: 842795

Article Last Modified on 3/29/2007

-

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:

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 &quot;Symptoms&quot; 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
 Log on to the computer as an administrator. Click Start, click Run, type regedit in the Open box, and then click OK. The Registry Editor window appears. Locate the following registry subkey:

 Right-click Eventlog, and then click Permissions. The Permissions for Eventlog dialog box appears. Click Advanced. The Advanced Security Settings for Eventlog dialog box appears. In the Name column, double-click the Users group. The Permission Entry for Eventlog dialog box appears.</li> Select the Set Value check box, select the Create Subkey check box, and then click OK.</li> Quit Registry Editor, and then log off from the administrator account.</li> Log on to the computer as a regular user.</li> 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.</li></ol>

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.

<div class="moreinformation_section">

Steps to reproduce the behavior
<ol> Create a regular user account: <ol style="list-style-type: lower-alpha;"> Log on to your computer as an administrator.</li> Right-click My Computer, and then click Manage. The Computer Management window appears.</li> Under System Tools, expand Local Users and Groups.</li> Right-click Users, and then click New User. The New User dialog box appears.</li> 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.</li> Clear the User must change password at next logon check box, select the Password never expires check box, and then click Create.</li> Click Close to close the New User dialog box.</li></ol> </li> Add the user to the Debugger User group: <ol style="list-style-type: lower-alpha;"> Under Local Users and Groups, click Users.</li> 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.</li> Click the Member Of tab, and then click Add. The Select Groups dialog box appears.</li> <li>Click Advanced. The Select Groups dialog box appears.</li> <li>Click Find Now. The available groups appear in the Select Groups dialog box.</li> <li>In the Name (RDN) column, click Debugger Users, and then click OK.</li> <li>In the Selected Groups dialog box, click OK.</li> <li>In the properties dialog box of the new account, click OK.</li> <li>Log off from the administrator account.</li></ol> </li> <li>Create a Windows application: <ol style="list-style-type: lower-alpha;"> <li>Log on to the computer by using the regular user account that you created in &quot;Step 1 - Create a regular user account&quot;.</li> <li>Start Visual Studio .NET.</li> <li>On the File menu, point to New, and then click Project. The New Project dialog box appears.</li> <li>Under Project Types, click Visual C# Projects.</li> <li>Under Templates, click Windows Application.</li> <li>In the Name box, type the name of a project, and then click OK. By default, the Form1 form appears.</li> <li>Right-click the Form1 form, and then click View Code. The Form1.cs file appears.</li> <li> 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 description for Form1. ///    public class Form1 : System.Windows.Forms.Form {       private System.Diagnostics.EventLog eventLog1; private System.Windows.Forms.Button button1; ///        /// Required designer variable. ///        private System.ComponentModel.Container components = null;

public Form1 {           //            // Required for Windows Form Designer support //           InitializeComponent;

if(!EventLog.SourceExists(&quot;Source1&quot;)) {               EventLog.CreateEventSource(&quot;Source1&quot;,&quot;MyLog1&quot;); }           eventLog1.Source = &quot;Source1&quot;; eventLog1.Log = &quot;MyLog1&quot;; //           // TODO: Add any constructor code after InitializeComponent call //       }        ///         /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

#region Windows Form Designer generated code ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        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 = &quot;button1&quot;; this.button1.TabIndex = 0; this.button1.Text = &quot;button1&quot;; 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 = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit; this.ResumeLayout(false);

}       #endregion

///        /// The main entry point for the application. ///        [STAThread] static void Main {           Application.Run(new Form1); }       private void button1_Click(object sender, System.EventArgs e)        { eventLog1.WriteEntry(&quot;The Button1 Is Clicked&quot;); }   } } </li> <li>On the File menu, click Save Form1.cs to save the file.</li></ol> </li> <li>Run the project: <ol style="list-style-type: lower-alpha;"> <li>On the Build menu, click Build Solution.</li> <li>Run the project. You may receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol> </li></ol>

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.

<div class="references_section">