如何在.NET 中打开保存的事件日志存档?

发布于 2024-09-05 00:11:31 字数 138 浏览 10 评论 0原文

我已使用 System.Diagnostics.EventLog 查看本地计算机上的日志。但是,我想打开已保存的事件日志存档(.evt 或 .evtx)并查看已保存文件中包含的日志。我只需要查看与文件中的日志关联的时间戳、消息、来源等。这可以在 C# 中完成吗?

I have used the System.Diagnostics.EventLog to view the logs on the local computer. However, I would like to open a saved event log archive (.evt or .evtx) and view the logs that are contained in the saved file. I just need to view timestamps, messages, sources, etc. associated with the logs in the file. Can this be done in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め可乐爱微笑 2024-09-12 00:11:31

查看 System.Diagnostics.Eventing.Reader 命名空间。特别是 EventLogQuery 类。

http://msdn.microsoft.com/en-我们/库/bb671200(v=VS.90).aspx

Check out the System.Diagnostics.Eventing.Reader namespace. Specifically the EventLogQuery class.

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

缱倦旧时光 2024-09-12 00:11:31

尝试使用 Microsoft 的 LogParser 工具。它可以使用类似 SQL 的选择语言从任何日志格式的日志中获取任何数据。它还可以在任何 .NET 应用程序中使用。 CSV日志的示例解析(我相信您可以将此代码用于EVT文件,只需稍加修改):

        public IList<LogRow> GetLog()
        {
            return Load("SELECT *, OUT_ROW_NUMBER() FROM logfile*.log WHERE Field2='Performance' ORDER BY Field1 ASC");
        }

    private static IList<LogRow> Load(string sql)
    {
        IEnumerable<string[]> log = ParseLog(sql);

        return Convert(log);
    }

    private static IList<LogRow> Convert(IEnumerable<string[]> log)
    {
        return log.Select(logRecord => new LogRow
                                           {
                                               TimeStamp = logRecord[2],
                                               Category = logRecord[3],
                                               Machine = logRecord[4],
                                               ThreadId = logRecord[5],
                                               ProcessId = logRecord[6],
                                               ProcessName = logRecord[7],
                                               DomainName = logRecord[8],
                                               Message = logRecord[9],
                                               Number = logRecord[10]
                                           }).ToList();
    }


        private static IEnumerable<string[]> ParseLog(string query)
        {
            var records = new LogQueryClassClass().Execute(
                query,
                new COMCSVInputContextClass { headerRow = false, iTsFormat = "yyyy-MM-dd HH:mm:ss.fff" });
            var entries = new List<string[]>();

            while (!records.atEnd())
            {
                entries.Add(records.getRecord().toNativeString("CSVseparator").Split(
                                new[] { "CSVseparator" },
                                StringSplitOptions.None));
                records.moveNext();
            }

            records.close();
            return entries;
        }

Try LogParser tool from Microsoft. It can fetch any data from logs of any log format using SQL-like selecting language. It can also be used from any .NET application. The example parsing of CSV logs (I believe you can use this code for EVT files with small modifications):

        public IList<LogRow> GetLog()
        {
            return Load("SELECT *, OUT_ROW_NUMBER() FROM logfile*.log WHERE Field2='Performance' ORDER BY Field1 ASC");
        }

    private static IList<LogRow> Load(string sql)
    {
        IEnumerable<string[]> log = ParseLog(sql);

        return Convert(log);
    }

    private static IList<LogRow> Convert(IEnumerable<string[]> log)
    {
        return log.Select(logRecord => new LogRow
                                           {
                                               TimeStamp = logRecord[2],
                                               Category = logRecord[3],
                                               Machine = logRecord[4],
                                               ThreadId = logRecord[5],
                                               ProcessId = logRecord[6],
                                               ProcessName = logRecord[7],
                                               DomainName = logRecord[8],
                                               Message = logRecord[9],
                                               Number = logRecord[10]
                                           }).ToList();
    }


        private static IEnumerable<string[]> ParseLog(string query)
        {
            var records = new LogQueryClassClass().Execute(
                query,
                new COMCSVInputContextClass { headerRow = false, iTsFormat = "yyyy-MM-dd HH:mm:ss.fff" });
            var entries = new List<string[]>();

            while (!records.atEnd())
            {
                entries.Add(records.getRecord().toNativeString("CSVseparator").Split(
                                new[] { "CSVseparator" },
                                StringSplitOptions.None));
                records.moveNext();
            }

            records.close();
            return entries;
        }
千笙结 2024-09-12 00:11:31

如果您的目的是读取保存的日志,我们可以使用 EventLogReader 来完成。它基本上需要两个参数 - 文件名(如文件路径),第二个参数指定路径类型。作为参考,假设您有一个已保存的 .evtx 文件 - temp.evtx,您可以按以下方式阅读它:

new EventLogReader(filepath, PathType.Filepath);

这为您提供了一个事件日志阅读器,可用于读取事件。更进一步,如果您想从中读取内容,我们可以使用 Properties,它基本上是一个字符串列表。可以阅读它、解析它并获取您需要的任何信息。

我同意我们没有办法像使用 EventLogEntry 那样直接获取所有详细信息。尽管如此,我们只需要进行一些解析即可使用事件记录获取我们需要的内容。

If your intention is to read the saved logs, we can do it using EventLogReader. It basically takes two parameters - filename (as in file path), and second parameter specifying path type. For your reference, say you have a saved .evtx file - temp.evtx, you can read it as in:

new EventLogReader(filepath, PathType.Filepath);

This gives you an event log reader, which can be used to read events. And further more, if you would want to read content out of it, we can use Properties which is basically a list of string. Can read it, parse it, and get whatever information you need.

I agree that we don't have the provision to directly get all the details as like what we get using EventLogEntry. Never the less, we just need to have some parsing done to get what we need using event record.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文