** 如何用 ** ** 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.NET 或 C# 来创建一个名为 ** 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
1<runinstaller(true)> _
2
3Public Class MyEventLogInstaller
4
5Inherits Installer
6
7Private myEventLogInstaller As EventLogInstaller
8
9Public Sub New()
10
11' Create an instance of 'EventLogInstaller'.
12
13myEventLogInstaller = New EventLogInstaller()
14
15' Set the 'Source' of the event log, to be created.
16
17myEventLogInstaller.Source = "TEST"
18
19' Set the 'Log' that the source is created in.
20
21myEventLogInstaller.Log = "Application"
22
23' Add myEventLogInstaller to 'InstallerCollection'.
24
25Installers.Add(myEventLogInstaller)
26
27End Sub
28
29End Class
30
31** Visual C# .NET Sample **
32
33using System;
34
35using System.Diagnostics;
36
37using System.ComponentModel;
38
39using System.Configuration.Install;
40
41namespace EventLogSourceInstaller
42
43{
44
45[RunInstaller(true)]
46
47public class MyEventLogInstaller : Installer
48
49{
50
51private EventLogInstaller myEventLogInstaller;
52
53public MyEventLogInstaller()
54
55{
56
57//Create Instance of EventLogInstaller
58
59myEventLogInstaller = new EventLogInstaller();
60
61// Set the Source of Event Log, to be created.
62
63myEventLogInstaller.Source = "TEST";
64
65// Set the Log that source is created in
66
67myEventLogInstaller.Log = "Application";
68
69// Add myEventLogInstaller to the Installers Collection.
70
71Installers.Add(myEventLogInstaller);
72
73}
74
75}
76
77}
78
795. 生成此项目,得到 EventLogSourceInstaller.dll 。
80
816. 打开 Visual Studio .NET 命令提示,转到 EventLogSourceInstaller.dll 所在目录。
82
837. 运行此命令来创建“事件来源”: ** InstallUtil EventLogSourceInstaller.dll **
84
85** 更详尽的信息 **
86
87我们通过一个创建一个 Web Application 来重现以上错误以及解决此问题。
88
891. 使用 VB.Net 或 C# 建立一个 Asp.net Web Application 。
90
912. 在 WebForm1.aspx 中的代码替换为以下代码:
92
93** Visual Basic .NET Sample **
@ Page Language="vb" AutoEventWireup="true"
@ Import namespace="System.Diagnostics"
1
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3
4<html>
5<script language="VB" runat="server">
6
7Sub WriteEvent_Click(Src As Object, e As EventArgs)
8
9Dim ev As New EventLog("Application")
10
11' Event's Source name
12
13ev.Source = "TEST"
14
15EventLog.CreateEventSource(ev.Source, "Application")
16
17Try
18
19ev.WriteEntry(TextBox1.Text)
20
21Catch b as exception
22
23Response.write ("WriteEntry " & b.message & "<br>")
24
25End Try
26
27ev = Nothing
28
29End Sub
30
31</script>
32<body>
33<form id="Form1" runat="server">
34
35Event message:
36
37<asp:textbox id="TextBox1" runat="server" width="233px"></asp:textbox>
38<asp:button id="Button1" name="Button1" onclick="WriteEvent_Click" runat="server" text="Write to event log"></asp:button>
39</form>
40</body>
41</html>
42
43** Visual C# .NET Sample **
@ Page Language="c#" AutoEventWireup="true"
@ Import namespace="System.Diagnostics"
1
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3
4<html>
5<script language="C#" runat="server">
6
7void WriteEvent_Click(Object Src, EventArgs e)
8
9{
10
11EventLog ev = new EventLog("Application");
12
13// Event's Source name
14
15ev.Source = "TEST";
16
17EventLog.CreateEventSource(ev.Source, "Application");
18
19try
20
21{
22
23ev.WriteEntry(TextBox1.Text);
24
25}
26
27catch (Exception b)
28
29{
30
31Response.Write("WriteEntry " + b.Message + "<br>");
32
33}
34
35ev = null;
36
37}
38
39</script>
40<body>
41<form id="Form1" runat="server">
42
43Event message:
44
45<asp:textbox id="TextBox1" runat="server" width="233px"></asp:textbox>
46<asp:button id="Button1" name="Button1" onclick="WriteEvent_Click" runat="server" text="Write to event log"></asp:button>
47</form>
48</body>
49</html>
50
513. 按 F5 启动此项目。
52
534. 在 ** TextBox ** 输入一些字符,然后点击 ** Write to Event Log ** 。
54
555. 在上面“现象”部分中提到的错误消息会出现。
56
576. 要解决此问题,在 Webform1.aspx 将下面这行代码注释
58EventLog.CreateEventSource(ev.Source, "Application");
59
607. 重新启动此项目。
61
62** 参考 **
63
64要取得更详尽的信息,请访问 微软网站:
65
66http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughCreatingEventLogInstallers.asp
67
68http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticseventlogclasstopic.asp</runinstaller(true)>