笔记14
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace EventLogTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEventLogTest_Click(object sender, EventArgs e)
{
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "Application";
//sEvent = "Sample Event";
//if (!EventLog.SourceExists(sSource))
// EventLog.CreateEventSource(sSource, sLog);
//EventLog.WriteEntry(sSource, sEvent);
//EventLog.WriteEntry(sSource, sEvent,
//EventLogEntryType.Warning, 234);
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "李涛";
//sEvent = "hello, word!";
//if (!EventLog.SourceExists(sSource))
// EventLog.CreateEventSource(sSource, sLog);
//EventLog.WriteEntry(sSource, sEvent);
//EventLog.WriteEntry(sSource, sEvent,
//EventLogEntryType.Warning, 234);
string sSource; //事件源
string sLog; //事件日志(名),又称为日志名。一个日志名可以对应多个事件源。
string sEvent; //消息
sSource = "litao application";
sLog = "李涛";
sEvent = "hello, word!";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234);
}
private void btnEventLogTest2_Click(object sender, EventArgs e)
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
}
}
//创建一个示例应用程序并写入事件日志中:
//1. 打开 Visual Studio .NET
//2. 在 Microsoft C# 中新建控制台应用程序。Visual C# .NET 为您创建一个公用类,以及一个空的 Main() 方法。
//3. 请确保项目至少引用了 System.dll。
//4. 对 System 和 System.Diagnostics 名称空间使用 using 指令,这样,在后面的代码中就不需要限定这些名称空间中的声明了。这些语句必须放在所有其他声明之前。
//using System;
//using System.Diagnostics;
//5. 若要写入事件日志,需要提供以下几条信息: 您的消息、要写入的日志名(如果不存在,就会创建一个名称)以及一个表示事件源的字符串。 某种源只能在一个事件日志中记录,因此,如果要在多个日志中写入消息,必须定义多个源。
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "Application";
//sEvent = "Sample Event";
//6. 有了所有这些信息后,第一步是使用 EventLog 类的两个静态方法先检查您的事件源是否存在,如果不存在,则创建与特定事件日志关联的事件源。 如果指定的日志名不存在,则在写入第一个条目时自动创建该名称。 如果没有为 CreateEventSource 方法提供日志名,则默认指定为应用程序日志。
//if (!EventLog.SourceExists(sSource))
//EventLog.CreateEventSource(sSource,sLog);
//7. 若要将消息写入事件日志,需使用静态方法 EventLog.WriteEntry(有多个不同的重载版本)。 在下面的代码中给出了最简单的方法,只需提供源字符串和您的消息,而比较复杂的方法还需提供事件 ID 和事件类型。
//EventLog.WriteEntry(sSource,sEvent);
//EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);
//8. 保存并运行代码,然后在事件查看器中检查应用程序日志,以查看您的新事件。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace EventLogTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEventLogTest_Click(object sender, EventArgs e)
{
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "Application";
//sEvent = "Sample Event";
//if (!EventLog.SourceExists(sSource))
// EventLog.CreateEventSource(sSource, sLog);
//EventLog.WriteEntry(sSource, sEvent);
//EventLog.WriteEntry(sSource, sEvent,
//EventLogEntryType.Warning, 234);
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "李涛";
//sEvent = "hello, word!";
//if (!EventLog.SourceExists(sSource))
// EventLog.CreateEventSource(sSource, sLog);
//EventLog.WriteEntry(sSource, sEvent);
//EventLog.WriteEntry(sSource, sEvent,
//EventLogEntryType.Warning, 234);
string sSource; //事件源
string sLog; //事件日志(名),又称为日志名。一个日志名可以对应多个事件源。
string sEvent; //消息
sSource = "litao application";
sLog = "李涛";
sEvent = "hello, word!";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234);
}
private void btnEventLogTest2_Click(object sender, EventArgs e)
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
}
}
//创建一个示例应用程序并写入事件日志中:
//1. 打开 Visual Studio .NET
//2. 在 Microsoft C# 中新建控制台应用程序。Visual C# .NET 为您创建一个公用类,以及一个空的 Main() 方法。
//3. 请确保项目至少引用了 System.dll。
//4. 对 System 和 System.Diagnostics 名称空间使用 using 指令,这样,在后面的代码中就不需要限定这些名称空间中的声明了。这些语句必须放在所有其他声明之前。
//using System;
//using System.Diagnostics;
//5. 若要写入事件日志,需要提供以下几条信息: 您的消息、要写入的日志名(如果不存在,就会创建一个名称)以及一个表示事件源的字符串。 某种源只能在一个事件日志中记录,因此,如果要在多个日志中写入消息,必须定义多个源。
//string sSource;
//string sLog;
//string sEvent;
//sSource = "dotNET Sample App";
//sLog = "Application";
//sEvent = "Sample Event";
//6. 有了所有这些信息后,第一步是使用 EventLog 类的两个静态方法先检查您的事件源是否存在,如果不存在,则创建与特定事件日志关联的事件源。 如果指定的日志名不存在,则在写入第一个条目时自动创建该名称。 如果没有为 CreateEventSource 方法提供日志名,则默认指定为应用程序日志。
//if (!EventLog.SourceExists(sSource))
//EventLog.CreateEventSource(sSource,sLog);
//7. 若要将消息写入事件日志,需使用静态方法 EventLog.WriteEntry(有多个不同的重载版本)。 在下面的代码中给出了最简单的方法,只需提供源字符串和您的消息,而比较复杂的方法还需提供事件 ID 和事件类型。
//EventLog.WriteEntry(sSource,sEvent);
//EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);
//8. 保存并运行代码,然后在事件查看器中检查应用程序日志,以查看您的新事件。

浙公网安备 33010602011771号