第二个方法
在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
<RunInstaller(True)> _
Public Class MyEventLogInstaller
Inherits Installer
** 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
{
** 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 "