This article contains information about editing the registry. Before editing the registry, it is important to understand how to restore the registry if a problem occurs. For information about how to restore the registry, view the "Restoring the Registry" Help topic in Regedit.exe, or the "Restoring Registry Keys" Help topic in Regedt32.exe.
Phenomenon
When you use ASP.NET to write a new "event source" to the event log, you may get the following error message: System.Security.SecurityException: The requested registry access is not allowed.
The reason why
the ASP.NET process is running
isThe default user is ASPNET (NetworkService under IIS6.0), and this user does not have permission to create an "event source".
Note onthe solution
: (Microsoft will not say much if you want to scare you, such as editing the registry will cause the system to crash). If you need to solve this problem, you must create an "event source" by a user with administrator rights before you run this Asp.net program. There are several methods for creating "event sources".
The first method
uses the following steps to create an "Event Source" under "Application Log" in the registry editor
1. Click "Start" and then "Run".
2. Enter "regedit" in the "Open" box.
3. Find the following subkey:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventlogApplication
4. Right-click "Application", click "New" and then click "Item"
5. Rename this new item to "Test"
6. Close Registry Editor
The second method
has an EventLogInstaller class in the System.Diagnostics namespace. It creates and configures event logs that your application reads and writes at runtime. Through the following steps, we can use the EventLogInstaller class to create an "event industry source"
1. Use VB.NET or C# to create a "class library" called EventLogSourceInstaller.
2. Add a reference to System.Configuration.Install.dll, in the project.
3. Rename the automatically generated Class.VbClass.cs to MyEventLogInstaller.vbMyEventLogInstaller.cs.
4. Replace the contents in MyEventLogInstaller.vb or MyEventLogInstaller.cs with the following code:
Visual Basic .NET Sample
ImportsSystem.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..computerDownloadFilesarticle27, 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..computerDownloadFilesarticle27, 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);
}
}
}
5. Build this project and get EventLogSourceInstaller.dll.
6. Open the Visual Studio .NET command prompt and go to the directory where EventLogSourceInstaller.dll is located.
7. Run this command to create the "event source": InstallUtil EventLogSourceInstaller.dll
More detailed information
We reproduce the above error and solve this problem by creating a Web Application.
1. Use VB.Net or C# to build an Asp.net Web Application.
2. Replace the code in WebForm1.aspx with the following 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..computerDownloadFilesarticle27, e As EventArgs)
Dim ev As New EventLog("Application")
'Event's Source name
ev.Source = "TEST"
EventLog.CreateEventSource(ev.Source..computerDownloadFilesarticle27, "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..computerDownloadFilesarticle27, EventArgs e)
{
EventLog ev = new EventLog("Application");
//Event's Source name
ev.Source = "TEST";
EventLog.CreateEventSource(ev.Source..computerDownloadFilesarticle27, "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" onclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
</form>
</body>
</HTML>
3. Press F5 to start this project.
4. Enter some characters in the TextBox and click Write to Event Log.
5. The error message mentioned in the "Symptoms" section above appears.
6. To solve this problem, comment the following line of code in Webform1.aspx
EventLog.CreateEventSource(ev.Source..computerDownloadFilesarticle27, "Application");
7. Restart this project.
http://www.cnblogs.com/niit007/archive/2006/08/13/475510.html