【转】ASP.NET Web应用程序写EventLog出错的解决方案

最近在对应一个ASP.NET Web应用程序Bug的时候,为了调查运行环境中具体的Bug于是查阅了一下错误日志。

这个程序的错误日志是直接保存在一个文本文件中的,结果发现就算是出错也不会留下任何痕迹。

一调试,竟然是把错误日志的保存路径给写错了。My God!

把路径改了,转念一想下次出错再不保存怎么办?谁知道真正的环境里会出什么状况呢。

于是乎,出错了就直接把错误日志写到服务器的EventLog上吧。

三下五除二改完了就等着好消息呢。

可是左等右等还是没有日志留下来,Bug还是照旧。

一查,嘿,不让写EventLog。

网上一查,果然已经有前车之鉴。微软官方上也有解决方法。

按照第一个方法做了一下,改了服务器的注册表就好用了。

第二个就没尝试了。

解决问题的文章就摘下来做个经验保留。

地址:http://support.microsoft.com/?scid=kb;zh-cn;329291&spid=810&sid=58#appliesto

原文:

症状 使用 ASP.NET 在事件日志中创建一个新的事件源时,您可能会收到下面的错误信息:System.Security.SecurityException:Requested registry access is not allowed.

原因 默认情况下,ASP.NET 工作进程的用户令牌是 ASPNET(或者,对于 Internet 信息服务 [IIS] 6.0 上运行的应用程序是 NetworkService)。由于您的帐户不具有创建事件源的正确用户权限,会出现“症状”部分中的问题。

解决方案


警告 :注册表编辑器使用不当可导致严重问题,从而可能需要重新安装操作系统。Microsoft 不能保证您可以解决因注册表编辑器使用不当而导致的问题。使用注册表编辑器需要您自担风险。 要解决此问题,在您运行 ASP.NET Web 应用程序之前,拥有管理权限的用户必须创建事件源。要创建事件源,请使用下列方法之一。

第一种方法

在注册表编辑器中,在应用程序 事件日志下创建一个事件源。为此,请执行下列步骤:

  1. 单击“开始”,然后单击“运行”。
  2. 在“打开”文本框中,键入 regedit
  3. 找到以下注册表子项:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
  4. 右键单击“Application”子项,指向“新建”,然后单击“项”。
  5. 键入 TEST 作为该项的名称。
  6. 关闭注册表编辑器。
第二种方法

System.Diagnostics 名称空间中的 EventLogInstaller 类允许您安装和配置一个事件日志,您的应用程序在运行时可以读取或写入该事件日志。您可以使用 EventLogInstaller 创建一个事件源。为此,请执行下列步骤:

  1. 使 用 Microsoft Visual Basic .NET 或 Microsoft Visual C# .NET 创建一个新的名为 EventLogSourceInstaller 的“类库”。默认情况下,将创建“Class1.vb”文件或“Class1.cs”文件。
  2. 在解决方案资源管理器中,右键单击“EventLogSourceInstaller”,然后单击“添加引用”。
  3. 在“添加引用”对话框中,双击“System.Configuration.Install.dll”,然后单击“确定”。
  4. 将 Class1.vb\Class1.cs 重命名为 MyEventLogInstaller.vb\MyEventLogInstaller.cs。
  5. 用以下示例代码替换 MyEventLogInstaller.vb 或 MyEventLogInstaller.cs 中现有的代码:

Visual Basic .NET 示例

 
 
  1. Imports System.Diagnostics  
  2. Imports System.Configuration.Install  
  3. Imports System.ComponentModel  
  4.   
  5. <RunInstaller(True)> _  
  6. Public Class MyEventLogInstaller  
  7.     Inherits Installer  
  8.     Private myEventLogInstaller As EventLogInstaller  
  9.   
  10.     Public Sub New()  
  11.         ' Create an instance of 'EventLogInstaller'.  
  12.         myEventLogInstaller = New EventLogInstaller()  
  13.         ' Set the 'Source' of the event log, to be created.  
  14.         myEventLogInstaller.Source = "TEST"  
  15.         ' Set the 'Log' that the source is created in.  
  16.         myEventLogInstaller.Log = "Application"  
  17.         ' Add myEventLogInstaller to 'InstallerCollection'.  
  18.         Installers.Add(myEventLogInstaller)  
  19.     End Sub   
  20. End Class   
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 示例

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





  6. 在“生成”菜单中,单击“生成解决方案”以生成“EventLogSourceInstaller.dll”。
  7.打开“Visual Studio .NET 命令提示符”。
  8.在该命令提示符下,更改到“EventLogSourceInstaller.dll”所在的文件夹。
  9.运行以下命令以生成 EventSource: InstallUtil EventLogSourceInstaller.dll



重现此问题的步骤

  1. 使用 Visual Basic .NET 或 Visual C# .NET 创建一个新的“ASP.NET Web 应用程序”。默认情况下,将创建“WebForm1.aspx”文件。
  2. 在“WebForm1.aspx”的 HTML 视图中,用以下示例代码替换现有代码:

Visual Basic .NET 示例

  1. <%@ Page Language="vb" AutoEventWireup="true" %>  
  2. <%@ Import namespace="System.Diagnostics" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  4. <HTML>  
  5.     <mce:script language="VB" runat="server"><!--  
  6.     Sub WriteEvent_Click(Src As Object, e As EventArgs)  
  7.     Dim ev As New EventLog("Application")  
  8.     ' Event's Source name  
  9.     ev.Source = "TEST"   
  10.       
  11.     EventLog.CreateEventSource(ev.Source, "Application")  
  12.       
  13. Try  
  14.      ev.WriteEntry(TextBox1.Text)  
  15.     Catch b as exception  
  16.      Response.write ("WriteEntry " & b.message & "<br>")  
  17.     End Try  
  18.     ev = Nothing  
  19.     End Sub  
  20.       
  21. // --></mce:script>  
  22.     <body>  
  23.         <form id="Form1" runat="server">  
  24.             Event message:   
  25.             <asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>  
  26.             <asp:button id="Button1" οnclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>  
  27.         </form>  
  28.     </body>  
  29. </HTML>  
<%@ Page Language="vb" AutoEventWireup="true" %> <%@ Import namespace="System.Diagnostics" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <mce: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 // --></mce:script> <body> <form id="Form1" runat="server"> Event message: <asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox> <asp:button id="Button1" οnclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button> </form> </body> </HTML>

Visual C# .NET 示例

  1. <%@ Page Language="c#" AutoEventWireup="true" %>  
  2. <%@ Import namespace="System.Diagnostics" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  4. <HTML>  
  5.     <mce:script language="C#" runat="server"><!--  
  6.     void WriteEvent_Click(Object Src, EventArgs e)  
  7.     {  
  8.     EventLog ev = new EventLog("Application");  
  9.     // Event's Source name  
  10.     ev.Source = "TEST";    
  11.       
  12.     EventLog.CreateEventSource(ev.Source, "Application");  
  13.             try  
  14.             {  
  15.                 ev.WriteEntry(TextBox1.Text);  
  16.             }  
  17.             catch (Exception b)  
  18.             {  
  19.                 Response.Write("WriteEntry " + b.Message + "<br>");  
  20.             }  
  21.             ev = null;  
  22.     }  
  23.       
  24. // --></mce:script>  
  25.     <body>  
  26.         <form id="Form1" runat="server">  
  27.             Event message:   
  28.             <asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox>  
  29.             <asp:button id="Button1" οnclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button>  
  30.         </form>  
  31.     </body>  
  32. </HTML>  
<%@ Page Language="c#" AutoEventWireup="true" %> <%@ Import namespace="System.Diagnostics" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <mce: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; } // --></mce:script> <body> <form id="Form1" runat="server"> Event message: <asp:textbox id="TextBox1" runat="server" Width="233px"></asp:textbox> <asp:button id="Button1" οnclick="WriteEvent_Click" runat="server" NAME="Button1" text="Write to event log"></asp:button> </form> </body> </HTML>

    3.  在“调试”菜单上,单击“开始”以在浏览器中查看“WebForm1.aspx”页面。
    4.  在“TextBox”中键入一些文字,然后单击“写入事件日志”。
    5.  本文“症状”部分讨论的错误信息将会出现。
    6.  要解决此问题,请创建一个如“解决方案”部分所讨论的事件源,并在“WebForm1.aspx”中给下列代码添加注释:

  1. EventLog.CreateEventSource(ev.Source, "Application")  
EventLog.CreateEventSource(ev.Source, "Application")
    7.  重复步骤 3 和 4。
这篇文章中的信息适用于:
  • Microsoft ASP.NET 1.1
  • Microsoft Visual Basic .NET 2003 标准版
  • Microsoft Visual C# .NET 2003 标准版
  • Microsoft ASP.NET 1.0
  • Microsoft Visual .NET 2002 标准版
  • Microsoft Visual C# .NET 2002 标准版
  • Microsoft Internet Information Services 5.0
  • Microsoft Internet Information Services 6.0

转载于:https://www.cnblogs.com/sedao/archive/2009/09/21/1571310.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值