📄 eventlog.cs
字号:
using System;
using System.Xml;
using System.Reflection;
//using System.Data;
using System.IO;
namespace OpenNETCF.Diagnostics
{
public delegate void EntryWrittenEventHandler(object sender, EventLogEntry e);
/// <summary>
/// Class is similar to the System.Diagnostics.EventLog in the full framework with a few differences.
/// 1. Since CE.Net and PPC have no event log we write the log to the application root directory as an XML file
/// 2. Every Application will have it's own log. There will not be one log system/device wide
/// 3. Attempted to keep as close as possible to the full framework class but some things are missing
/// </summary>
public class EventLog
{
/// <summary>
/// Public event to notify listeners that the log has changed
/// </summary>
public event EventHandler LogChanged;
/// <summary>
/// Public event to notify listeners that the log display name has changed
/// </summary>
public event EventHandler LogDisplayNameChanged;
/// <summary>
/// Public event to notify listeners that the log has been closed
/// </summary>
public event EventHandler LogClosed;
/// <summary>
/// Public event to notify listeners that the log has been cleared
/// </summary>
public event EventHandler LogCleared;
/// <summary>
/// Public event to notify listeners that the source has changed
/// </summary>
public event EventHandler SourceChanged;
/// <summary>
/// Notifies listeners if there is a new log in the eventLog
/// </summary>
public event EventHandler EventLogAdded;
/// <summary>
/// Occurs when an entry is written to an event log on the local computer
/// </summary>
public event EntryWrittenEventHandler EntryWritten;
private bool enableRaisingEvents = false;
private EventLogWriterType eventLogWriterType = EventLogWriterType.XML;
private IEventLogWriter eventLogWriter;
/// <summary>
/// Overloaded constructor were a custom IEventLogWriter can be specified
/// </summary>
/// <param name="log">Indicates the log item</param>
/// <param name="source">Indicates what logged the event</param>
/// <param name="customEventLogWriter">Custom event log writter which implements IEventLogWriter</param>
public EventLog(string log, string source, IEventLogWriter customEventLogWriter)
{
string logFileName = "";
string logPath = "";
//Setup the file and path
if(logFileName.Length==0)
{
logFileName = Assembly.GetCallingAssembly().GetName().CodeBase + ".Log";
logFileName = logFileName.Substring(logFileName.LastIndexOf("\\")+1);
}
if(logPath.Length==0)
{
logPath = Assembly.GetCallingAssembly().GetName().CodeBase;
logPath = Path.GetDirectoryName(logPath) + "\\";
}
//Load the log file
this.eventLogWriterType = EventLogWriterType.Custom;
this.eventLogWriter = customEventLogWriter;
this.CreateEventLogWriter(log, source, logPath, logFileName);
}
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="log">Indicates the log item</param>
/// <param name="source">Indicates what logged the event</param>
/// <param name="type"></param>
public EventLog(string log, string source, EventLogWriterType type)
{
string logFileName = "";
string logPath = "";
//Setup the file and path
if(logFileName.Length==0)
{
logFileName = Assembly.GetCallingAssembly().GetName().CodeBase + ".Log";
logFileName = logFileName.Substring(logFileName.LastIndexOf("\\")+1);
}
if(logPath.Length==0)
{
logPath = Assembly.GetCallingAssembly().GetName().CodeBase;
logPath = Path.GetDirectoryName(logPath) + "\\";
}
//Load the log file
this.eventLogWriterType = type;
this.CreateEventLogWriter(log, source, logPath, logFileName);
}
/// <summary>
/// Overloaded constructor
/// </summary>
/// <param name="log">Indicates the log item</param>
/// <param name="source">Indicates what logged the event</param>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <param name="type"></param>
public EventLog(string log, string source,string path, string fileName, EventLogWriterType type)
{
//Load the log file
this.eventLogWriterType = type;
this.CreateEventLogWriter(log,source,path,fileName);
}
#region Public Properties
/// <summary>
/// Gets or sets a value indicating whether the EventLog receives EntryWritten event notifications.
/// </summary>
public bool EnableRaisingEvents
{
get
{
return this.enableRaisingEvents;
}
set
{
this.enableRaisingEvents = value;
}
}
/// <summary>
/// Gets the contents of the event log.
/// </summary>
public EventLogEntryCollection Entries
{
get
{
return this.eventLogWriter.Entries;
}
}
/// <summary>
/// Gets or sets the name of the log to read from or write to.
/// </summary>
public string Log
{
get
{
return this.eventLogWriter.Log;
}
set
{
if(value.Length>0)
{
this.eventLogWriter.Log = value;
this.OnLogChanged();
}
else
throw new Exception("Log name cannot be blank.");
}
}
/// <summary>
/// Gets the event log's friendly name.
/// </summary>
public string LogDisplayName
{
get
{
return this.eventLogWriter.LogDisplayName;
}
set
{
this.eventLogWriter.LogDisplayName = value;
this.OnLogDisplayNameChanged();
}
}
/// <summary>
/// Gets or sets the source name to register and use when writing to the event log.
/// </summary>
public string Source
{
get
{
return this.eventLogWriter.Source;
}
set
{
if(value.Length>0)
{
this.eventLogWriter.Source = value;
this.OnSourceChanged();
}
else
throw new Exception("Source cannot be blank.");
}
}
/// <summary>
/// Gets the file name the log is stored under. Defaults to the calling assembly name with ".Log" appended
/// </summary>
public string LogFileName
{
get
{
return this.eventLogWriter.LogFileName;
}
}
/// <summary>
/// Gets the path of where the log file is stored
/// </summary>
public string LogPath
{
get
{
return this.eventLogWriter.LogPath;
}
}
/// <summary>
/// Gets the eventLogWriterType
/// </summary>
public EventLogWriterType EventLogWriterType
{
get
{
return this.eventLogWriterType;
}
}
#endregion
#region Public Methods
/// <summary>
/// Removes an event log from the local file.
/// </summary>
/// <param name="logName">The name of the log to delete.</param>
public void Delete(string logName)
{
if(this.eventLogWriter.Log.Equals(logName))
throw new Exception("Cannot delete the log because it is currently open. You must close the log before deleting.");
this.eventLogWriter.Delete(logName);
}
/// <summary>
/// Searches for all event logs on the local file and creates an array of EventLog objects that contain the list.
/// </summary>
/// <returns>An array of type EventLog that represents the logs on the local computer.</returns>
public EventLog[] GetEventLogs()
{
return this.eventLogWriter.GetEventLogs();
}
/// <summary>
/// Determines whether the log exists on the local file.
/// </summary>
/// <param name="logName">The name of the log to search for.</param>
/// <remarks>The full framework defines this method as static. Since this is not a system wide log but an application specific log this method will only search for a Log Item with the current XML file.</remarks>
public bool Exists(string logName)
{
return this.eventLogWriter.Exists(logName);
}
/// <summary>
/// Removes all entries from the event log.
/// </summary>
public void Clear()
{
//Check to see if the log was closed
if(this.eventLogWriter.Log.Length==0)
throw new Exception("Unable to clear log because it has been closed.");
this.eventLogWriter.Clear();
this.OnLogCleared();
}
/// <summary>
/// Closes the event log and releases read and write handles.
/// </summary>
public void Close()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -