Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows中访问和查询事件日志_C#_Arrays_Event Log - Fatal编程技术网

C# 在Windows中访问和查询事件日志

C# 在Windows中访问和查询事件日志,c#,arrays,event-log,C#,Arrays,Event Log,我想知道如何才能访问事件日志条目。我有一个客户机-服务器应用程序,它执行起来没有问题。我要查找的是id为1149的所有日志实例。此日志是远程连接条目的一部分。我带了一段代码,在这里 string logType = "System"; string str = ""; EventLog ev = new EventLog(logType, System.Environment.MachineName); int LastLogToShow = ev.Entries.Count; if (Last

我想知道如何才能访问事件日志条目。我有一个客户机-服务器应用程序,它执行起来没有问题。我要查找的是id为1149的所有日志实例。此日志是远程连接条目的一部分。我带了一段代码,在这里

string logType = "System";
string str = "";
EventLog ev = new EventLog(logType, System.Environment.MachineName);
int LastLogToShow = ev.Entries.Count;
if (LastLogToShow <= 0)
    Console.WriteLine("No Event Logs in the Log :" + logType);

// Read the last 2 records in the specified log. 
int i;
for (i = ev.Entries.Count; i >= LastLogToShow - 1000 ; i--)
{
    EventLogEntry CurrentEntry = ev.Entries[i];
    if (CurrentEntry.InstanceId == 1149)
    {
        str += "Event type: " + CurrentEntry.EntryType.ToString() + "\n" +
               "Event Message: " + CurrentEntry.Message + CurrentEntry + "\n" +
               "Event Time: " + CurrentEntry.TimeGenerated.ToShortTimeString() + "\n" +
               "Event : " + CurrentEntry.UserName +"\n" +"\n";
    }
}
ev.Close();
return str;
(i=ev.Entries.Count;i>=LastLogToShow-1000;i--)的
导致了您的错误。我真的不明白你在这里想做什么。例如,如果您的条目少于1000条,则您的
i
可以为负数。当使用负值作为数组的索引时,将得到“索引超出边界异常”。当您试图仅处理最后2条记录时(如for循环上面的注释所示),您应该使用以下方法:

for (i = ev.Entries.Count - 1; i >= ev.Entries.Count - 2; i--)
当然,您仍然需要检查是否有2个以上的条目,因为如果有0个条目,代码仍将进入for循环,并尝试使用负索引访问数组:

if(ev.Entries.Count < 2)
  return str;
for (i = ev.Entries.Count - 1; i >= ev.Entries.Count - 2; i--)
if(ev.Entries.Count<2)
返回str;
对于(i=ev.Entries.Count-1;i>=ev.Entries.Count-2;i--)

编辑:也注意到,即使有1000条以上的记录,当您第一次进入for循环时,也会有
ev.Entries[ev.Entries.Count]
。由于数组索引是从零开始的,因此必须从计数中减去1才能得到数组的最后一个元素。

只需将i等于ev.Entries.count-1即可。 i=(ev.Entries.Count-1)

我强烈建议您为此使用C#Linq

添加此命名空间

using System.Linq;
Linq在处理数据的方式上非常类似于SQL。就你而言:

List<string> stringLogs = 
    ev.Entries
        .Where(t => t.InstanceId == 1149)
        .Select(t => GenerateLogString(t))
        .ToList();

public string GenerateLogString(EventLogEntry CurrentEntry)
{
    return
        string.Format("Event type: {0}\nEvent Message: {1}\nEvent Time: {2}\nEvent: {3}\n",
            CurrentEntry.EntryType.ToString(),
            CurrentEntry.Message + CurrentEntry,
            CurrentEntry.TimeGenerated.ToShortTimeString(),
            CurrentEntry.UserName)
}
如果您想选择前两个日志(如您的评论所示),请在查询中添加.Take(2),如下所示

List<string> stringLogs = 
    ev.Entries
        .Where(t => t.InstanceId == 1149)
        .Take(2)
        .Select(t => GenerateLogString(t))
        .ToList();
列出字符串日志=
ev.条目
.其中(t=>t.InstanceId==1149)
.采取(2)
.选择(t=>GenerateLogString(t))
.ToList();

谢谢,我将使用LINQ,一旦我彻底测试了程序,它的性能会更好,对吗?当然,性能会更好,实现也会更安全。如果您使用我的工具,请更新正确答案。:)无论有多少个元素,此方法也会起作用。由于您从不使用索引请求元素,因此不可能出现超出范围的异常。虽然在您的情况下,这并不是一个大问题,但LINQ在处理非常大的数据集时,可以防止应用程序上出现任何大规模负载,这是不可思议的。如果需要帮助,请打电话给我。:)
string str = string.Join("/n", stringLogs);
List<string> stringLogs = 
    ev.Entries
        .Where(t => t.InstanceId == 1149)
        .Take(2)
        .Select(t => GenerateLogString(t))
        .ToList();