C#操作Windows系統日志信息


  利用C#編程,查看系統日志,介紹兩個日志類:EventLog和EventLogEntry類,以及與系統日志進行交互。

.NET框架類庫提供了EventLog類和EventLogEntry類與系統日志進行交互.二者屬於System.Diagnostics命名空間

首先聲明一變量:private EventLogEntryCollection eventCollection 代表系統日志的集合。 

    EventLog類的屬性主要有:

  Entris返回一個EventLogEntryCollection型值,代表事件日志的內容

  Log 獲取或者返回日志的名稱,其中應用程序日志是Application,系統日志是System,安全日志是Security,默認值為空字符串。

  LogDisplayName 獲取事件日志的友好名稱 

    MachineName 獲取或設置在其上讀取或寫入事件的計算機名稱

  Source 獲取或設置在寫入事件日志時要注冊和使用的源名稱

EventEntryCollection類定義EventLogEntry實例集合的大小和枚舉數。EventLogEntry類的一些主要屬性如下:

  Category 獲取與該項的CategoryNumber對應的文本 ;

    CategoryNumber 獲取該項的類別號;  

  Data 獲取與該項對應的二進制數據;

EntryType 獲取該項的事件類型,其值屬於EventLogEntryType 枚舉,這個枚舉的主要成員如下:

  Error 錯誤事件,它指示用戶應該知道的嚴重問題,比如功能或數據丟失;

  FailureAudit 失敗審核事件.它指示當審核訪問嘗試失敗,比如打開文件的嘗試;

  Information 信息事件.它指示重要,成功的事件;

  SuccessAudit 成功審核事件.它指示當審核訪問嘗試成功,比如成功登錄時發生的安全事件;

  Warning 警告事件.它指示並不立即具有重要性的問題,但此問題可能表示將來會導致問題的條件;

  EventID 獲取此事件項的應用程序特定事件標識符;

  Index 獲取該項在事件日志中的索引;

  MachineName 獲取在產生該項的計算機的名稱;

  Message 獲取與該事件的本地化消息;

  ReplacementStrings 獲取對應該項替換字符串;

  Source 獲取生成該事件的應用程序的名稱;

  TimeGenerated 獲取生成該事件的本地時間;

  TimeWritten 獲取在日志寫入該事件的本地時間;

  UserName 獲取負責該事件的用戶的名稱。

 代碼示例如下:

using System;

using System.Diagnostics;

namespace LogView

{

public class SysLogView

{

private EventLogEntryCollection eventCollection;

private EventLog systemEvent;

public SysLogView()

{

systemEvent=new  EventLog ();

systemEvent.Log="System";

eventCollection=systemEvent.Entries;

}

private void LoadEventLog(int c)

{

EventLog systemEvent=new EventLog();

systemEvent.Log="System";

eventCollection=systemEvent.Entries;

int length=eventCollection.Count;

EventLogEntry entry=eventCollection[c];

string [] title={

entry.EntryType.ToString(),

entry.TimeGenerated.ToLongDateString(),

entry.TimeGenerated.ToLongTimeString(),

entry.Source,

entry.Category,

entry.EventID.ToString(),

entry.UserName,

entry.MachineName

};

for(int j=0;j<title.Count;j++)

{

Console.WriteLine(title[j]);

}

Console.WriteLine("\n"+entry.Message );

}

private string DisplayEventCount()

{

return (length.ToString());

}

public static void Main(string [] args)

{

SysLogView slv=new SysLogView();
if(args.length==1)
{
int x=Convert.ToInt32(args[0]);
slv.LoadEventLog(x);
}
else
{
Console.WriteLine(" Event count:"+slv.DisplayEventCount());
}
}
}
}

C# 創建系統日志

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Log
{
class LogWirter
{
/// <summary>
/// 事件源名稱
/// </summary>
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "test";
eventLogType = EventLogEntryType.Error;
}

/// <summary>
/// 消息事件源名稱
/// </summary>
public string EventSourceName
{
set { eventSourceName = value; }
}

/// <summary>
/// 消息事件類型
/// </summary>
public EventLogEntryType EventLogType
{
set { eventLogType = value; }
}

/// <summary>
/// 寫入系統日志
/// </summary>
/// <param name="message">事件內容</param>
public void LogEvent(string message)
{
if (!EventLog.SourceExists(eventSourceName))
{
EventLog.CreateEventSource(eventSourceName, "Application");
}
EventLog.WriteEntry(eventSourceName, message, EventLogEntryType.Error);
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM