本文包含有关编辑注册表的信息编辑注册表之前务必先了解在发生问题时如何还原注册表有关如何还原注册表的信息请查看 Regeditexe 中的还原注册表帮助主题或 Regedtexe 中的还原注册表项帮助主题
现象
当你使用aspnet 向事件日志中写入一个新的事件来源时可能会得到如下错误消息 SystemSecuritySecurityException: 不允许所请求的注册表访问权
原因
运行aspnet进程的默认怅户是ASPNET(在IIS下面是NetworkService)而此用户并没有权限来创建事件来源
解决办法
注意(编辑注册表会导致系统崩溃之类的微软吓你的话就不多说)如果你需要解决此问题在你运行此Aspnet程序之前则必须要由具有管理员权限的用户来创建一个事件来源下面有几个方法用来创建 事件来源
第一个方法
使用下列步骤在注册表编辑中在应用程序日志下面创建一个事件来源
. 点击开始再点击运行
. 在打开框中输入regedit
. 找到下列子键
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
. 右击Application点击新建再点项
. 将此新建项重命名为Test
. 关闭注册表编辑器
第二个方法
在SystemDiagnostics命名空间中有一个EventLogInstaller类它能够创建和配置你的应用程序在运时要读写的事件日志通过下列步骤我们能够使用EventLogInstaller类来创建一个事件业源
. 用VBNET或C#来创建一个名为EventLogSourceInstaller的类库
. 在项目中添加对SystemConfigurationInstalldll的引用
. 将自动产生的ClassVb\Classcs更命名为MyEventLogInstallervb\MyEventLogInstallercs
. 在MyEventLogInstallervb 或 MyEventLogInstallercs中的内容替换为以下代码
Visual Basic NET Sample
Imports SystemDiagnostics
Imports SystemConfigurationInstall
Imports SystemComponentModel
<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
myEventLogInstallerSource = TEST
Set the Log that the source is created in
myEventLogInstallerLog = Application
Add myEventLogInstaller to InstallerCollection
InstallersAdd(myEventLogInstaller)
End Sub
End Class
Visual C# NET Sample
using System;
using SystemDiagnostics;
using SystemComponentModel;
using SystemConfigurationInstall;
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
myEventLogInstallerSource = TEST;
// Set the Log that source is created in
myEventLogInstallerLog = Application;
// Add myEventLogInstaller to the Installers Collection
InstallersAdd(myEventLogInstaller);
}
}
}
. 生成此项目得到EventLogSourceInstallerdll
. 打开Visual Studio NET 命令提示转到EventLogSourceInstallerdll所在目录
. 运行此命令来创建事件来源InstallUtil EventLogSourceInstallerdll
更详尽的信息
我们通过一个创建一个Web Application来重现以上错误以及解决此问题
. 使用VBNet或C#建立一个Aspnet Web Application
. 在WebFormaspx中的代码替换为以下代码
Visual Basic NET Sample
<%@ Page Language=vb AutoEventWireup=true %>
<%@ Import namespace=SystemDiagnostics %>
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
<HTML>
<script language=VB runat=server>
Sub WriteEvent_Click(Src As Object e As EventArgs)
Dim ev As New EventLog(Application)
Events Source name
evSource = TEST
EventLogCreateEventSource(evSource Application)
Try
evWriteEntry(TextBoxText)
Catch b as exception
Responsewrite (WriteEntry & bmessage & <br>)
End Try
ev = Nothing
End Sub
</script>
<body>
<form id=Form runat=server>
Event message:
<asp:textbox id=TextBox runat=server Width=px></asp:textbox>
<asp:button id=Button onclick=WriteEvent_Click runat=server NAME=Button text=Write to event log></asp:button>
</form>
</body>
</HTML>
Visual C# NET Sample
<%@ Page Language=c# AutoEventWireup=true %>
<%@ Import namespace=SystemDiagnostics %>
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
<HTML>
<script language=C# runat=server>
void WriteEvent_Click(Object Src EventArgs e)
{
EventLog ev = new EventLog(Application);
// Events Source name
evSource = TEST;
EventLogCreateEventSource(evSource Application);
try
{
evWriteEntry(TextBoxText);
}
catch (Exception b)
{
ResponseWrite(WriteEntry + bMessage + <br>);
}
ev = null;
}
</script>
<body>
<form id=Form runat=server>
Event message:
<asp:textbox id=TextBox runat=server Width=px></asp:textbox>
<asp:button id=Button onclick=WriteEvent_Click runat=server NAME=Button text=Write to event log></asp:button>
</form>
</body>
</HTML>
. 按F启动此项目
. 在TextBox输入一些字符然后点击Write to Event Log
. 在上面现象部分中提到的错误消息会出现
. 要解决此问题在Webformaspx将下面这行代码注释
EventLogCreateEventSource(evSource Application);
. 重新启动此项目