如何用在asp.net中寫入事件日誌

如何用asp.net寫事件日誌

來源:http://support.microsoft.com/default.aspx?scid=kb;en-us;329291

關鍵字:asp.net 事件日誌 錯誤消息

 

重要事項

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

 

現象

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

 

原因

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

 

解決辦法

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

 

第一個方法

 

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

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

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

3.  找到下列子鍵:

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Application

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

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

6.  關閉註冊表編輯器

 

第二個方法

 

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

1.  VB.NETC#來創建一個名爲EventLogSourceInstaller的“類庫”。

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

3.  將自動產生的Class.Vb/Class.cs更命名爲MyEventLogInstaller.vb/MyEventLogInstaller.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.NetC#建立一個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://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughCreatingEventLogInstallers.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticseventlogclasstopic.asp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章