惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 黃偉榮

Web Project的檔案共用小技巧 IoC的中繼器:CommonServiceLocator UTF8Encoding與BOM Temporary Post Used For Theme Detection (d4b0aefa-c88e-4957-bba7-b367d1bfa042 - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 寫CodedUI時如何尋找控制項的小技巧 Visual Studio 2010 Feature Packs 2之Silverlight自動化測試 Moles - Isolation framework for .NET(假.Net)介紹 [小技巧]Entity Framework強型別Include C#仿Oracle Decode,將ValueType對應成String - 黃偉榮 - 博客园 Visual Studio 單元測試的3種Initialize與Cleanup jQuery套件-檢查頁面的欄位是否有變更 jQuery自製Plugin-Bind事件函式時檢查有沒有Bind過 ASP.NET MVC TempData使用心得 Visual Stuiod 自訂檔案比較合并工具 [小技巧]自動化測試時NLog的訊息輸出到測試結果中 小技巧:專案切換32與64位元組件 Linq小技巧:日期處理 Unit Test小技巧 : DateTime的Stub 解決TFS Build Asp.Net Mvc開啟MvcBuildViews後無法載入組件問題
用EventLogReader查詢特殊EventLog
黃偉榮 · 2010-10-20 · via 博客园 - 黃偉榮

最近在寫Windows Task Scheduler相關的功能,想取某Task的EventLog,但用System.Diagnostics.EventLog只能取得基本的EventLog,後來發現應該要使用System.Diagnostics.Eventing.Reader.EventLogReader,而且EventLogReader可以下查詢,過濾出所需的資料。

System.Diagnostics.EventLog的限制

我用Reflector看過原始碼,看到EventLog這個Class,只能取得
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog 這個機碼下的根EventLog,不巧Task Scheduler不在這之中。

image

圖1 EventLog的機碼

image

圖2 剛好就是事件檢視器中,比較豐富ICON的EventLog

如果要取得EventLog剛好不在之中的話,請改用System.Diagnostics.Eventing.Reader.EventLogReader

System.Diagnostics.Eventing.Reader.EventLogReader使用說明

建立EventLogReader,有二個方法

  • LogName
  • FilePath

預設值是LogName,如果不知道這二個值,可以用事件檢視器取得

在要想要看EventLog上按右鍵

image

全名就是LogName,記錄檔路徑就是FilePath

image

不含查詢的語法建構式

var eventLog = new EventLogReader("Microsoft-Windows-TaskScheduler/Operational");
var eventLog = new EventLogReader(Path.Combine(Environment.SystemDirectory, @"Winevt\Logs\Microsoft-Windows-TaskScheduler%4Operational.evtx"), PathType.FilePath);

但是因為這一個EventLog是記錄所有Task的Log,勢必要加查詢條件(如果你想全部取出在自己寫Code分,也行),這時建構式要換成EventLogQuery

//如果是屬性用@如 *[System/Provider/@Name=\"A\"]
string queryStr = string.Format("*[EventData/Data=\"{0}\"]", task.Path);
EventLogQuery query = new EventLogQuery("Microsoft-Windows-TaskScheduler/Operational", PathType.LogName, queryStr);
EventLogReader eventLog = new EventLogReader(query);

如果不清楚查詢的元素有那些,可以隨便打開一個Log,看它的事件內容,查詢的元素完全等於它的XML

image

圖3 事件內容

範例

public static List<EventRecord> GetHistory(this IRegisteredTask task, int take = 30)
{
    string queryStr = string.Format("*[EventData/Data=\"{0}\"]", task.Path);
    EventLogQuery query = new EventLogQuery("Microsoft-Windows-TaskScheduler/Operational", PathType.LogName, queryStr);
    EventLogReader eventLog = new EventLogReader(query);
    var result = new List<EventRecord>();
    EventRecord log = eventLog.ReadEvent();
    while (log != null)
    {
        result.Add(log);
        if (result.Count >= take)
        {
            break;
        }

        log = eventLog.ReadEvent();
    }

    return result;
}

NOTE:

如果要取TaskScheduler的EventLog,別忘了啟用記錄。
image

測試平台

Windows 7、Windows Server 2008