top
Loading...
如何用在ASP.NET中寫入事件日志
天極IT資訊短信服務 電腦小技巧
資費:包月5元
手機:
介紹:細處著手,巧處用功。高手和菜鳥之間的差別就是:高手什么都知道,菜鳥知道一些。電腦小技巧收集最新奇招高招,讓你輕松踏上高手之路。(首月免費)

文包含有關編輯注冊表的信息。編輯注冊表之前,務必先了解在發生問題時如何還原注冊表。有關如何還原注冊表的信息,請查看 Regedit.exe 中的“還原注冊表”幫助主題,或 Regedt32.exe 中的“還原注冊表項”幫助主題。

現象

當你使用asp.net 向事件日志中寫入一個新的“事件來源”時,可能會得到如下錯誤消息: System.Security.SecurityException: 不允許所請求的注冊表訪問權

原因

運行asp.net進程的默認悵戶是ASPNET(在IIS6.0下面是NetworkService),而此用戶并沒有權限來創建“事件來源”。

解決辦法

注意:(編輯注冊表會導致系統崩潰之類的微軟嚇你的話就不多說)。如果你需要解決此問題,在你運行此Asp.net程序之前,則必須要由具有管理員權限的用戶來創建一個“事件來源”。下面有幾個方法用來創建 “事件來源”。

第一個方法

使用下列步驟在注冊表編輯中在”應用程序日志”下面創建一個“事件來源”

1. 點擊“開始”,再點擊“運行”。

2. 在“打開”框中輸入“regedit”。

3. 找到下列子鍵:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventlogApplication

4. 右擊“Application”點擊“新建”再點“項”

5. 將此新建項重命名為“Test”

6. 關閉注冊表編輯器

第二個方法

在System.Diagnostics命名空間中有一個EventLogInstaller類。它能夠創建和配置你的應用程序在運時要讀寫的事件日志。通過下列步驟,我們能夠使用EventLogInstaller類來創建一個“事件業源”

1. 用VB.NET或C#來創建一個名為EventLogSourceInstaller的“類庫”。

2. 在項目中添加對System.Configuration.Install.dll,的引用。

3. 將自動產生的Class.VbClass.cs更命名為MyEventLogInstaller.vbMyEventLogInstaller.cs。

4. 在MyEventLogInstaller.vb 或 MyEventLogInstaller.cs中的內容替換為以下代碼:

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);
}
}
}

5. 生成此項目,得到EventLogSourceInstaller.dll。

6. 打開Visual Studio .NET 命令提示,轉到EventLogSourceInstaller.dll所在目錄。

7. 運行此命令來創建“事件來源”:InstallUtil EventLogSourceInstaller.dll

更詳盡的信息

我們通過一個創建一個Web Application來重現以上錯誤以及解決此問題。

1. 使用VB.Net或C#建立一個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, 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" onclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>
</form>
</body>
</HTML>

3. 按F5啟動此項目。

4. 在TextBox輸入一些字符,然后點擊Write to Event Log。

5. 在上面“現象”部分中提到的錯誤消息會出現。

6. 要解決此問題,在Webform1.aspx將下面這行代碼注釋

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

7. 重新啟動此項目。

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗