文包含有關(guān)編輯注冊(cè)表的信息。編輯注冊(cè)表之前,務(wù)必先了解在發(fā)生問題時(shí)如何還原注冊(cè)表。有關(guān)如何還原注冊(cè)表的信息,請(qǐng)查看 Regedit.exe 中的“還原注冊(cè)表”幫助主題,或 Regedt32.exe 中的“還原注冊(cè)表項(xiàng)”幫助主題。
現(xiàn)象
當(dāng)你使用asp.net 向事件日志中寫入一個(gè)新的“事件來源”時(shí),可能會(huì)得到如下錯(cuò)誤消息: System.Security.SecurityException: 不允許所請(qǐng)求的注冊(cè)表訪問權(quán)
原因
運(yùn)行asp.net進(jìn)程的默認(rèn)悵戶是ASPNET(在IIS6.0下面是NetworkService),而此用戶并沒有權(quán)限來創(chuàng)建“事件來源”。
解決辦法
注意:(編輯注冊(cè)表會(huì)導(dǎo)致系統(tǒng)崩潰之類的微軟嚇你的話就不多說)。如果你需要解決此問題,在你運(yùn)行此Asp.net程序之前,則必須要由具有管理員權(quán)限的用戶來創(chuàng)建一個(gè)“事件來源”。下面有幾個(gè)方法用來創(chuàng)建 “事件來源”。
第一個(gè)方法
使用下列步驟在注冊(cè)表編輯中在”應(yīng)用程序日志”下面創(chuàng)建一個(gè)“事件來源”
1. 點(diǎn)擊“開始”,再點(diǎn)擊“運(yùn)行”。
2. 在“打開”框中輸入“regedit”。
3. 找到下列子鍵:
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/application
4. 右擊“Application”點(diǎn)擊“新建”再點(diǎn)“項(xiàng)”
5. 將此新建項(xiàng)重命名為“Test”
6. 關(guān)閉注冊(cè)表編輯器
第二個(gè)方法
在System.Diagnostics命名空間中有一個(gè)EventLogInstaller類。它能夠創(chuàng)建和配置你的應(yīng)用程序在運(yùn)時(shí)要讀寫的事件日志。通過下列步驟,我們能夠使用EventLogInstaller類來創(chuàng)建一個(gè)“事件業(yè)源”
1. 用VB.NET或C#來創(chuàng)建一個(gè)名為EventLogSourceInstaller的“類庫”。
2. 在項(xiàng)目中添加對(duì)System.Configuration.Install.dll,的引用。
3. 將自動(dòng)產(chǎn)生的Class.Vb/Class.cs更命名為MyEventLogInstaller.vb/MyEventLogInstaller.cs。
4. 在MyEventLogInstaller.vb 或 MyEventLogInstaller.cs中的內(nèi)容替換為以下代碼:
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../computer/DownloadFiles/article/27/, 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../computer/DownloadFiles/article/27/, 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. 生成此項(xiàng)目,得到EventLogSourceInstaller.dll。
6. 打開Visual Studio .NET 命令提示,轉(zhuǎn)到EventLogSourceInstaller.dll所在目錄。
7. 運(yùn)行此命令來創(chuàng)建“事件來源”:InstallUtil EventLogSourceInstaller.dll
更詳盡的信息
我們通過一個(gè)創(chuàng)建一個(gè)Web Application來重現(xiàn)以上錯(cuò)誤以及解決此問題。
1. 使用VB.Net或C#建立一個(gè)Asp.net Web Application。
2. 在WebForm1.aspx中的代碼替換為以下代碼:
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../computer/DownloadFiles/article/27/, e As EventArgs)
Dim ev As New EventLog("Application")
' Event's Source name
ev.Source = "TEST"
EventLog.CreateEventSource(ev.Source../computer/DownloadFiles/article/27/, "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../computer/DownloadFiles/article/27/, EventArgs e)
{
EventLog ev = new EventLog("Application");
// Event's Source name
ev.Source = "TEST";
EventLog.CreateEventSource(ev.Source../computer/DownloadFiles/article/27/, "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. 按F5啟動(dòng)此項(xiàng)目。
4. 在TextBox輸入一些字符,然后點(diǎn)擊Write to Event Log。
5. 在上面“現(xiàn)象”部分中提到的錯(cuò)誤消息會(huì)出現(xiàn)。
6. 要解決此問題,在Webform1.aspx將下面這行代碼注釋
EventLog.CreateEventSource(ev.Source../computer/DownloadFiles/article/27/, "Application");
7. 重新啟動(dòng)此項(xiàng)目。
http://m.survivalescaperooms.com/niit007/archive/2006/08/13/475510.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注