using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Data;
namespace EventsLog
{
public class LogWriter
{
/// <summary>
/// Write EventLog into ~/Log/Error.Log.
/// </summary>
/// <param name="message">Message</param>
public static void WriteToFile(string message)
{
WriteToFile(message, "Error.Log");
}
/// <summary>
/// Write EventLog to file of given path.
/// </summary>
/// <param name="message">Message</param>
/// <param name="path">Path of file</param>
public static void WriteToFile(string message, string path)
{
try
{
StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode);
sw.WriteLine("============================================================================");
sw.WriteLine("Time Generated:");
sw.WriteLine(" " + System.DateTime.Now.ToString());
sw.WriteLine("/r/nMessage:");
sw.WriteLine(message);
sw.WriteLine("/r/n/r/n/r/n");
//Must close the stream.
sw.Close();
}
catch { }
}
/// <summary>
/// Write EventLog of Error type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteErrorToReg(string message)
{
WriteToReg(message, EventLogEntryType.Error);
}
/// <summary>
/// Write EventLog of Information type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteInfoToReg(string message)
{
WriteToReg(message, EventLogEntryType.Information);
}
/// <summary>
/// Write EventLog of Warning type to register.
/// </summary>
/// <param name="message">Message</param>
public static void WriteWarningToReg(string message)
{
WriteToReg(message, EventLogEntryType.Warning);
}
/// <summary>
/// Write EventLog to register.
/// </summary>
/// <param name="message">Message</param>
/// <param name="type">Type
/// {
/// Error = EventLogEntryType.Error,
/// Information = EventLogEntryType.Information,
/// Warning = EventLogEntryType.Warning.
/// }</param>
public static void WriteToReg(string message, EventLogEntryType type)
{
try
{
//Get EventLog.
EventLog eventLog = getEventLog();
//Write message to EventLog.
eventLog.WriteEntry(message, type);
}
catch (Exception e)
{
WriteToFile(e.ToString());
}
}
/// <summary>
/// Read EventLog from register.
/// </summary>
/// <returns>Return DataTable with all EventLog info.</returns>
public static DataTable ReadFromReg()
{
//Get EventLog.
EventLog eventLog = getEventLog();
//Construct a DataTable Object,with 3 columns : EntryType、TimeGenerated、Message
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EntryType", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("TimeGenerated", System.Type.GetType("System.DateTime")));
dt.Columns.Add(new DataColumn("Message", System.Type.GetType("System.String")));
//Read EventLog,and add all to DataTable
foreach (EventLogEntry entry in eventLog.Entries)
{
dt.Rows.Add(new object[] { entry.EntryType, entry.TimeGenerated, entry.Message });
}
return dt;
}
/// <summary>
/// Clear EventLog from register.
/// </summary>
/// <returns>Return is whether successful.</returns>
public static bool ClearFromReg()
{
try
{
//Get EventLog.
EventLog eventLog = getEventLog();
//Clear all EventLog.
eventLog.Clear();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Get EventLog which source is "WebApplication".
/// </summary>
/// <returns>EventLog with special source.</returns>
private static EventLog getEventLog()
{
string source = "WebApplication";
//Make sure EventLog of special source exist.
if (!(EventLog.SourceExists(source)))
{
EventLog.CreateEventSource(source, "Application");
}
//New an EventLog Object, and set the source.
EventLog eventLog = new EventLog("Application");
eventLog.Source = source;
return eventLog;
}
}
}
本文介绍了一个用于写入和读取事件日志的实用类。该类提供了多种方法来记录不同类型的事件(如错误、警告和信息),并能将这些事件写入文件或系统注册表中。同时,也提供了清除事件日志和从注册表中读取事件日志的功能。

1673

被折叠的 条评论
为什么被折叠?



