用于远程计算机的EventLogQuery阅读器?

时间:2011-11-01 13:15:17

标签: c# event-log lan

我正在使用此代码从我的win7计算机上读取自己的事件日志。

 EventLogQuery eventsQuery = new EventLogQuery("Security", PathType.LogName, queryString);
 eventsQuery.ReverseDirection = true;
 EventLogReader logReader = new EventLogReader(eventsQuery);

然而 - 我需要阅读远程计算机的事件日志(Lan - 同一域名)

我该怎么做?

1 个答案:

答案 0 :(得分:15)

http://msdn.microsoft.com/en-us/library/bb671200(v=vs.90).aspx

public void QueryRemoteComputer()
    {
        string queryString = "*[System/Level=2]"; // XPATH Query
        SecureString pw = GetPassword();

        EventLogSession session = new EventLogSession(
            "RemoteComputerName",                               // Remote Computer
            "Domain",                                  // Domain
            "Username",                                // Username
            pw,
            SessionAuthentication.Default);

        pw.Dispose();

        // Query the Application log on the remote computer.
        EventLogQuery query = new EventLogQuery("Application", PathType.LogName, queryString);
        query.Session = session;

        try
        {
            EventLogReader logReader = new EventLogReader(query);

            // Display event info
            DisplayEventAndLogInformation(logReader);
        }
        catch (EventLogException e)
        {
            Console.WriteLine("Could not query the remote computer! " + e.Message);
            return;
        }
    }