您现在的位置是:首页 >

程序事件日志 如何用在ASP.NET中写入事件日志

火烧 2022-12-21 14:17:24 1062
如何用在ASP.NET中写入事件日志   本文包含有关编辑注册表的信息 编辑注册表之前 务必先了解在发生问题时如何还原注册表 有关如何还原注册表的信息 请查看 Regedit exe 中的 还原注册表

如何用在ASP.NET中写入事件日志  

  本文包含有关编辑注册表的信息 编辑注册表之前 务必先了解在发生问题时如何还原注册表 有关如何还原注册表的信息 请查看 Regedit exe 中的 还原注册表 帮助主题 或 Regedt exe 中的 还原注册表项 帮助主题

  现象

  当你使用asp net 向事件日志中写入一个新的 事件来源 时 可能会得到如下错误消息 System Security SecurityException: 不允许所请求的注册表访问权

  原因

  运行asp net进程的默认怅户是ASPNET(在IIS 下面是NeorkService) 而此用户并没有权限来创建 事件来源

  解决办法

  注意 (编辑注册表会导致系统崩溃之类的微软吓你的话就不多说) 如果你需要解决此问题 在你运行此Asp net程序之前 则必须要由具有管理员权限的用户来创建一个 事件来源 下面有几个方法用来创建 事件来源

  第一个方法

  使用下列步骤在注册表编辑中在 应用程序日志 下面创建一个 事件来源

    . 点击 开始 再点击 运行

    . 在 打开 框中输入 regedit

    . 找到下列子键

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventlogApplication

    . 右击 Application 点击 新建 再点 项

    . 将此新建项重命名为 Test

    . 关闭注册表编辑器  第二个方法

  在System Diagnostics命名空间中有一个EventLogInstaller类 它能够创建和配置你的应用程序在运时要读写的事件日志 通过下列步骤 我们能够使用EventLogInstaller类来创建一个 事件业源

   . 用VB NET或C#来创建一个名为EventLogSourceInstaller的 类库

   . 在项目中添加对System Configuration Install dll 的引用

   . 将自动产生的Class VbClass cs更命名为MyEventLogInstaller vbMyEventLogInstaller cs

   . 在MyEventLogInstaller vb 或 MyEventLogInstaller cs中的内容替换为以下代码

Visual Basic NET Sample

Imports System DiagnosticsImports System Configuration InstallImports System ComponentModel

<RunInstaller(True)> _Public Class MyEventLogInstallerInherits InstallerPrivate 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 Sampleusing System;using System Diagnostics;using System ComponentModel;using System Configuration Install;

namespace EventLogSourceInstaller {[RunInstaller(true)]public class MyEventLogInstaller : Installer{private EventLogInstaller myEventLogInstaller;

程序事件日志 如何用在ASP.NET中写入事件日志

public MyEventLogInstaller(){//Create Instance of EventLogInstallermyEventLogInstaller = new EventLogInstaller();

// Set the Source of Event Log to be created myEventLogInstaller Source = TEST ;

// Set the Log that source is created inmyEventLogInstaller Log = Application ;

// Add myEventLogInstaller to the Installers Collection Installers Add(myEventLogInstaller);}}} 

   . 生成此项目 得到EventLogSourceInstaller dll

   . 打开Visual Studio NET 命令提示 转到EventLogSourceInstaller dll所在目录

   . 运行此命令来创建 事件来源 InstallUtil EventLogSourceInstaller dll

  更详尽的信息

  我们通过一个创建一个Web Application来重现以上错误以及解决此问题

   . 使用VB Net或C#建立一个Asp net Web Application

   . 在WebForm aspx中的代码替换为以下代码

Visual Basic NET Sample

<%@ Page Language= vb AutoEventWireup= true %><%@ Import namespace= System Diagnostics %><!DOCTYPE HTML PUBLIC //W C//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 ) Event s Source nameev Source = TEST

EventLog CreateEventSource(ev Source Application )

Tryev WriteEntry(TextBox Text)Catch b as exceptionResponse write ( WriteEntry & b message & <br> )End Tryev = NothingEnd 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= System Diagnostics %><!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN ><HTML><script language= C# runat= server >void WriteEvent_Click(Object Src EventArgs e){EventLog ev = new EventLog( Application );// Event s Source nameev Source = TEST ;

EventLog CreateEventSource(ev Source Application );

try{ev WriteEntry(TextBox Text);}catch (Exception b){Response Write( WriteEntry + b Message + <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

   . 在上面 现象 部分中提到的错误消息会出现

   . 要解决此问题 在Webform aspx将下面这行代码注释

EventLog CreateEventSource(ev Source Application );

lishixinzhi/Article/program/net/201311/15435  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码