📄 xmleventlogwritter.cs.txt
字号:
att.Value = category.ToString();
nodeEventLog.Attributes.Append(att);
//rawData
att = this.xmlLog.CreateAttribute("rawData");
if(rawData == null)
rawData = new byte[0];
att.Value = System.Text.Encoding.Unicode.GetString(rawData,0,rawData.Length);
nodeEventLog.Attributes.Append(att);
//Add a GUID
GuidEx id = GuidEx.NewGuid();
att = this.xmlLog.CreateAttribute("id");
att.Value = id.ToString();
nodeEventLog.Attributes.Append(att);
//add index
int index = this.eventLogEntryCollection.Count+1;
att = this.xmlLog.CreateAttribute("index");
att.Value = index.ToString();
nodeEventLog.Attributes.Append(att);
//Time written
DateTime timeWritten = DateTime.Now;
att = this.xmlLog.CreateAttribute("timeWritten");
att.Value = timeWritten.ToString();
nodeEventLog.Attributes.Append(att);
//Add the eventLog to the xml
this.nodeLog.AppendChild(nodeEventLog);
//Add the eventlog to the collection
int lastEntry = this.eventLogEntryCollection.Add(new EventLogEntry(category,rawData,type,eventID,
index,machineName,message,
source,timeGenerated,timeWritten,userName,id.ToString()));
//Save the log file
this.SaveLogFile();
//Raise the event to listers
this.OnEntryWritten(this.eventLogEntryCollection[lastEntry]);
}
}
private void RemoveEventLog(string logName)
{
//Remove from the collection
if(this.eventLogCollection!=null)
{
foreach(EventLog e in this.eventLogCollection)
{
if(e.Log == logName)
{
this.eventLogCollection.Remove(e);
this.OnEventLogCollectionUpdated();
break;
}
}
}
}
/// <summary>
/// Raises the eventLogEntryWritten event to listeners
/// </summary>
/// <param name="e"></param>
private void OnEntryWritten(EventLogEntry e)
{
if(this.EntryWritten!=null)
this.EntryWritten(this,e);
}
private void OnEventLogCollectionUpdated()
{
if(this.EventLogCollectionUpdated!=null)
this.EventLogCollectionUpdated(this,EventArgs.Empty);
}
#endregion
#region IEventLogWritter Members
public string Source
{
get
{
return this.source;
}
set
{
if(value!=null && value!= "" && value!=this.source)
this.source = value;
}
}
public string Log
{
get
{
return this.log;
}
set
{
if(value!=null && value!= "" && value!=this.log)
{
this.log = value;
this.source = "";
this.logDisplayName = "";
//Check to see if the log node exists
XmlNode newLogNode = this.EventLogNode(value);
if(newLogNode==null)
{
//the node does not exists
this.nodeLog = this.CreateLogNode(this.log);
//add to the eventlog collection
if(this.eventLogCollection!=null)
{
EventLog e = new EventLog(log,"",this.logPath,this.logFileName, EventLogWritterType.XML);
if(!this.eventLogCollection.Contains(e))
{
this.eventLogCollection.Add(e);
this.OnEventLogCollectionUpdated();
}
}
}
else
{
//the node exists so set the new node
this.nodeLog = newLogNode;
this.logDisplayName = this.nodeLog.Attributes.GetNamedItem("logDisplayName").Value;
}
}
//Save the log file
this.SaveLogFile();
//Load the eventlog collection
this.LoadEventLogEntryCollection();
}
}
public string LogDisplayName
{
get
{
return this.logDisplayName;
}
set
{
if(value!=null && value!= "" && value!=this.logDisplayName)
{
this.logDisplayName = value;
//Set the logdisplayname in the node
this.NodeAttributeValue(this.nodeLog,"logDisplayName",value);
}
}
}
public string LogFileName
{
get
{
return this.logFileName;
}
}
public string LogPath
{
get
{
return this.logPath;
}
}
public void Delete(string logName)
{
XmlNodeList list = this.xmlLog.GetElementsByTagName("log");
foreach(XmlNode node in list)
{
string log = this.NodeAttributeValue(node,"name");
if(log.Equals(logName))
{
//remove eventlog from collection
this.RemoveEventLog(logName);
this.nodeEventLog.RemoveChild(node);
this.SaveLogFile();
break;
}
}
}
public void Clear()
{
//Clear all the log items from the xml
int y = this.nodeLog.ChildNodes.Count;
for(int x = 0; x<y;x++)
this.nodeLog.RemoveChild(this.nodeLog.ChildNodes[0]);
//Save the xml file
this.SaveLogFile();
//Re-init the eventLogCollection
this.eventLogEntryCollection = new EventLogEntryCollection();
}
public void Close()
{
this.SaveLogFile();
this.nodeLog = null;
this.log = "";
this.source ="";
this.logDisplayName = "";
this.eventLogEntryCollection = new EventLogEntryCollection();
}
public EventLog[] GetEventLogs()
{
if(this.eventLogCollection==null)
this.LoadEventLogCollection();
EventLog[] ret = (EventLog[])this.eventLogCollection.ToArray(typeof(EventLog));
return ret;
}
public EventLogEntryCollection Entries
{
get
{
return this.eventLogEntryCollection;
}
}
public bool Exists(string logName)
{
XmlNodeList list = this.xmlLog.GetElementsByTagName("log");
foreach(XmlNode node in list)
{
XmlAttribute att = (XmlAttribute)node.Attributes.GetNamedItem("name");
if(att!=null)
if(att.Value == logName)
return true;
}
return false;
}
public void WriteEntry(string message)
{
this.WriteEntry(this.source,message,EventLogEntryType.Information,0,0,null);
}
public void WriteEntry(string message, EventLogEntryType type)
{
this.WriteEntry(this.source,message,type,0,0,null);
}
public void WriteEntry(string source, string message)
{
this.WriteEntry(source,message,EventLogEntryType.Information,0,0,null);
}
public void WriteEntry(string message, EventLogEntryType type, Int32 eventID)
{
this.WriteEntry(this.source,message,type,eventID,0,null);
}
public void WriteEntry(string source, string message, EventLogEntryType type)
{
this.WriteEntry(source,message,type,0,0,null);
}
public void WriteEntry(string message, EventLogEntryType type, int eventID, short category)
{
this.WriteEntry(this.source,message,type,eventID,category,null);
}
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID)
{
this.WriteEntry(source,message,type,eventID,0,null);
}
public void WriteEntry(string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
{
this.WriteEntry(this.source,message,type,eventID,category,rawData);
}
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID, short category)
{
this.WriteEntry(source,message,type,eventID,category,null);
}
public void WriteEntry(string source, string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
{
this.WriteEntryToLog(source, message, type, eventID, category, rawData);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -