讀寫Evenlog
寫了函式方便使用
有空再來改寫
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Collections.Generic;
/// <summary>
/// AppLog 的摘要说明
/// </summary>
public class AppLog
{
//Use the standard TraceLevel values:
// 0 = off
// 1 = Error
// 2 = Warning
// 3 = Info
// 4 = Verbose -->
private static string CurMachineName = ".";
private static string CurSourceName = "IntraWeb";
private static string CurLogName = "System";
private System.Diagnostics.EventLog eventLog1;
[Flags]
public enum LogTypes
{
System,
Application,
Security
};
#region 建構子
public AppLog()
{
}
public AppLog(string SourceName)
{
CurSourceName = SourceName;
}
public AppLog(string SourceName, string LogName)
{
CurSourceName = SourceName;
CurLogName = LogName;
}
public AppLog(string SourceName, string LogName, string MachineName)
{
CurMachineName = MachineName;
CurSourceName = SourceName;
CurLogName = LogName;
}
#endregion
/// <summary>
/// 寫錯誤訊息.
/// <param name="message">The text to write to the log file or event log.</param>
/// </summary>
public void WriteError(String message)
{
//Defer to the helper function to log the message.
WriteLog(TraceLevel.Error, message);
}
/// <summary>
/// 寫警告訊息
/// <param name="message">The text to write to the log file or event log.</param>
/// </summary>
public void WriteWarning(String message)
{
//Defer to the helper function to log the message.
WriteLog(TraceLevel.Warning, message);
}
/// <summary>
/// 寫資訊訊息.
/// <param name="message">The text to write to the log file or event log.</param>
/// </summary>
public void WriteInfo(String message)
{
//Defer to the helper function to log the message.
WriteLog(TraceLevel.Info, message);
}
/// <summary>
/// 寫Evenlog.
/// </summary>
/// <param name="level">The level.</param>
/// <param name="messageText">The message text.</param>
private static void WriteLog(TraceLevel level, String messageText)
{
// Map the trace level to the corresponding event log attribute.
// Note that EventLogEntryType = 2 ^ (level - 1), but it is generally not
// considered good style to apply arithmetic operations to enum values.
EventLogEntryType LogEntryType;
try
{
switch (level)
{
case TraceLevel.Error:
LogEntryType = EventLogEntryType.Error;
break;
case TraceLevel.Warning:
LogEntryType = EventLogEntryType.Warning;
break;
case TraceLevel.Info:
LogEntryType = EventLogEntryType.Information;
break;
case TraceLevel.Verbose:
LogEntryType = EventLogEntryType.SuccessAudit;
break;
default:
LogEntryType = EventLogEntryType.SuccessAudit;
break;
}
EventLog CureventLog = new EventLog(CurLogName, CurMachineName, CurSourceName);
CureventLog.WriteEntry(messageText, LogEntryType);
}
catch (Exception e)
{
return;
} //Ignore any exceptions.
}
/// <summary>
/// 取得應用程式記錄檔.
/// </summary>
/// <returns></returns>
public string[] GetApplicationInfo()
{
List<string> result = new List<string>();
EventLog MySystemEvent = new EventLog();
MySystemEvent.Log = "Application";
EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
int Count = MyEventCollection.Count;
for (int i = 0; i < Count; i++)
{
EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
string[] SubItems ={ MyEntry.EntryType.ToString(),
MyEntry.TimeGenerated.ToLongDateString(),
MyEntry.TimeGenerated.ToLongTimeString(),
MyEntry.Source,
MyEntry.Category,
MyEntry.InstanceId.ToString(),
MyEntry.UserName,
MyEntry.Message,
MyEntry.MachineName
};
result.Add(string.Join("\t", SubItems));
}
return result.ToArray();
}
/// <summary>
/// 取得安全性記錄檔.(readonly)
/// </summary>
/// <returns></returns>
public string[] GetSecurityInfo()
{
List<string> result = new List<string>();
EventLog MySystemEvent = new EventLog();
MySystemEvent.Log = "Security";
EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
int Count = MyEventCollection.Count;
for (int i = 0; i < Count; i++)
{
EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
string[] SubItems ={ MyEntry.EntryType.ToString(),
MyEntry.TimeGenerated.ToLongDateString(),
MyEntry.TimeGenerated.ToLongTimeString(),
MyEntry.Source,
MyEntry.Category,
MyEntry.InstanceId.ToString(),
MyEntry.UserName,
MyEntry.Message,
MyEntry.MachineName
};
result.Add(string.Join("\t", SubItems));
}
return result.ToArray();
}
/// <summary>
/// 取得系統記錄檔.(readonly)
/// </summary>
/// <returns></returns>
public string[] GetSystemInfo()
{
List<string> result = new List<string>();
EventLog MySystemEvent = new EventLog();
MySystemEvent.Log = "System";
EventLogEntryCollection MyEventCollection = MySystemEvent.Entries;
int Count = MyEventCollection.Count;
for (int i = 0; i < Count; i++)
{
EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
string[] SubItems ={ MyEntry.EntryType.ToString(),
MyEntry.TimeGenerated.ToLongDateString(),
MyEntry.TimeGenerated.ToLongTimeString(),
MyEntry.Source,
MyEntry.Category,
MyEntry.InstanceId.ToString(),
MyEntry.UserName,
MyEntry.Message,
MyEntry.MachineName
};
result.Add(string.Join("\t", SubItems));
}
return result.ToArray();
}
/// <summary>
/// 建立新的記錄檔.
/// </summary>
/// <param name="LogName">Name of the log.</param>
/// <param name="SourceName">Name of the source.</param>
public void CreateNewLogCategory(string LogName, string SourceName)
{
if (EventLog.SourceExists(SourceName, CurMachineName))
{
EventLog.DeleteEventSource(SourceName);
EventLog.CreateEventSource(SourceName, LogName);
}
else
{
EventLog.CreateEventSource(SourceName, LogName);
}
eventLog1 = new EventLog(LogName, ".", SourceName);
}
public void SetEventLog(string LogName, string SourceName)
{
eventLog1 = new EventLog(LogName, ".", SourceName);
}
/// <summary>
/// 寫Evenlog.
/// </summary>
/// <param name="LogName">Name of the log.</param>
/// <param name="SorceName">Name of the sorce.</param>
/// <param name="messageText">The message text.</param>
/// <param name="LogEntryType">Type of the log entry.</param>
public void WriteLog(string LogName, string SorceName, string messageText, System.Diagnostics.EventLogEntryType LogEntryType)
{
EventLog CureventLog = new EventLog(LogName, CurMachineName, SorceName);
CureventLog.WriteEntry(messageText, LogEntryType);
}
/// <summary>
/// 刪除記錄檔是件來源紀錄.
/// </summary>
/// <param name="SourceName">Name of the source.</param>
public void DelEventSource(string SourceName)
{
if (EventLog.SourceExists(SourceName))
EventLog.DeleteEventSource(SourceName, CurMachineName);
}
/// <summary>
/// 刪除記錄檔.
/// </summary>
/// <param name="LogName">Name of the log.</param>
public void DelEventLog(string LogName)
{
EventLog.Delete(LogName);
}
}