PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve

转自:http://support.microsoft.com/default.aspx?scid=kb;en-us;329291

SYMPTOMS

<script type="text/javascript">loadTOCNode(1, 'symptoms');</script>
When you use ASP.NET to create a new event source in the event log, you may receive the following error message:
System.Security.SecurityException: Requested registry access is not allowed.

Back to the top

CAUSE

<script type="text/javascript">loadTOCNode(1, 'cause');</script>
By default, the user token of the ASP.NET worker process is ASPNET (or NetworkService for applications that run on Internet Information Services [IIS] 6.0). The problem in the "Symptoms" section occurs because your account does not have the correct user rights to create an event source.

Back to the top

RESOLUTION

<script type="text/javascript">loadTOCNode(1, 'resolution');</script>
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 (http://support.microsoft.com/kb/322756/) How to back up and restore the registry in Windows
To resolve this problem, a user who has administrative rights must create the event source before you run the ASP.NET Web Application. To create an event source, use one of the following approaches.
First Approach
<script type="text/javascript">loadTOCNode(3, 'resolution');</script>Create an event source under the Application event log in Registry Editor. To do this, follow these steps:
1.Click Start, and then click Run.
2.In the Open text box, type regedit.
3.Locate the following registry subkey:
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Application
4.Right-click the Application subkey, point to New, and then click Key.
5.Type TEST for the key name.
6.Close Registry Editor.
Second Approach
<script type="text/javascript">loadTOCNode(3, 'resolution');</script>The EventLogInstaller class in the System.Diagnostics namespace permits you to install and configure an event log that your application reads from or writes to while running. You can create an event source by using EventLogInstaller. To do this, follow these steps:
1.Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new Class Library named EventLogSourceInstaller. By default, the Class1.vb file or the Class1.cs file is created.
2.In Solution Explorer, right-click EventLogSourceInstaller, and then click Add References.
3.In the Add Reference dialog box, double-click System.Configuration.Install.dll, and then click OK.
4.Rename the Class1.vb/Class1.cs to MyEventLogInstaller.vb/MyEventLogInstaller.cs.
5.Replace the existing code in MyEventLogInstaller.vb or MyEventLogInstaller.cs with the following sample code:

Visual Basic .NET Sample
Imports System.Diagnostics
Imports System.Configuration.Install
Imports System.ComponentModel

<RunInstaller(True)> _
Public Class MyEventLogInstaller
Inherits Installer
Private myEventLogInstaller As EventLogInstaller

Public Sub New()
' Create an instance of 'EventLogInstaller'.
myEventLogInstaller = New EventLogInstaller()
' Set the 'Source' of the event log, to be created.
myEventLogInstaller.Source = "TEST"
' Set the 'Log' that the source is created in.
myEventLogInstaller.Log = "Application"
' Add myEventLogInstaller to 'InstallerCollection'.
Installers.Add(myEventLogInstaller)
End Sub
End Class
Visual C# .NET Sample
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;


namespace EventLogSourceInstaller
{
[RunInstaller(true)]
public class MyEventLogInstaller : Installer
{
private EventLogInstaller myEventLogInstaller;

public MyEventLogInstaller()
{
//Create Instance of EventLogInstaller
myEventLogInstaller = new EventLogInstaller();

// Set the Source of Event Log, to be created.
myEventLogInstaller.Source = "TEST";

// Set the Log that source is created in
myEventLogInstaller.Log = "Application";

// Add myEventLogInstaller to the Installers Collection.
Installers.Add(myEventLogInstaller);
}
}
}

6.On the Build menu, click Build Solution to create EventLogSourceInstaller.dll.
7.Open the Visual Studio .NET Command Prompt.
8.At the command prompt, change to the folder where EventLogSourceInstaller.dll is located.
9.Run the following command to create the EventSource:
InstallUtil EventLogSourceInstaller.dll

Back to the top

MORE INFORMATION

<script type="text/javascript">loadTOCNode(1, 'moreinformation');</script>

Steps to Reproduce the Behavior

<script type="text/javascript">loadTOCNode(2, 'moreinformation');</script>
1.Use Visual Basic .NET or Visual C# .NET to create a new ASP.NET Web Application. By default, WebForm1.aspx file is created.
2.In the HTML view of WebForm1.aspx, replace the existing code with the following sample code:

Visual Basic .NET Sample
<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import namespace="System.Diagnostics" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<script language="VB" runat="server">
Sub WriteEvent_Click(Src As Object, e As EventArgs)
Dim ev As New EventLog("Application")
' Event's Source name
ev.Source = "TEST"

EventLog.CreateEventSource(ev.Source, "Application")

Try
ev.WriteEntry(TextBox1.Text)
Catch b as exception
Response.write ("WriteEntry " & b.message & "<br>")
End Try
ev = Nothing
End Sub
</script>

<body>
<form id="Form1" runat="server">
Event message:
<asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>
<asp:button id="Button1" onclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
</form>
</body>
</HTML>
Visual C# .NET Sample
<%@ Page Language="c#" AutoEventWireup="true" %>
<%@ Import namespace="System.Diagnostics" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<script language="C#" runat="server">
void WriteEvent_Click(Object Src, EventArgs e)
{
EventLog ev = new EventLog("Application");
// Event's Source name
ev.Source = "TEST";

EventLog.CreateEventSource(ev.Source, "Application");

try
{
ev.WriteEntry(TextBox1.Text);
}
catch (Exception b)
{
Response.Write("WriteEntry " + b.Message + "<br>");
}
ev = null;
}
</script>

<body>
<form id="Form1" runat="server">
Event message:
<asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>
<asp:button id="Button1" οnclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
</form>
</body>
</HTML>
3.On the Debug menu, click Start to view the WebForm1.aspx page in the browser.
4.Type some text in TextBox, and then click Write to event log.
5.The error message that is discussed in the "Symptoms" section of this article appears.
6.To resolve this problem, create an Event Source as discussed in the "Resolution" section, and comment the following code in WebForm1.aspx :
EventLog.CreateEventSource(ev.Source, "Application")
7.Repeat steps 3 and 4.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值