本文包含有關編輯注冊表的信息編輯注冊表之前務必先了解在發生問題時如何還原注冊表有關如何還原注冊表的信息請查看 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);
. 重新啟動此項目
From:http://tw.wingwit.com/Article/program/net/201311/15435.html