【问题标题】:Log Parser - Query through MSUtill and Read Record through C# codes日志解析器 - 通过 MSUtill 查询和通过 C# 代码读取记录
【发布时间】:2012-05-09 15:11:48
【问题描述】:

我发现很多关于 Log Parser 的线程,但没有一个能满足我的愿望,

我需要在日志解析器上应用这个查询 从安全中选择 *

并获取事件行,然后逐行读取它们,知道我访问哪一列...

请帮忙...

应该关闭什么: 1.通过LogParser.dll查询Windows事件(使用MSUtill库) 2.获取并将行放入任何容器中 3.逐行读取 4.能够将行的每个单元格分开,并像访问事件ID列或消息列一样单独读取它们...


--提问

有一些东西,但我没有得到它们, 例如,这个人使用这个,取单行,但我需要多行,我想知道接下来要转换什么:|我需要使用什么类

LogQueryClass logger = new LogQueryClassClass();
            COMIISW3CInputContextClass inputContext = new COMIISW3CInputContextClassClass();
            string query = // i change it during test
            ILogRecord record = logger.Execute(query, inputContext).getRecord();

或其他使用:似乎也返回单个结果

// prepare LogParser Recordset & Record objects
            ILogRecordset rsLP = null;
            ILogRecord rowLP = null;

            LogQueryClassClass LogParser = null;
            COMW3CInputContextClassClass W3Clog = null;

            double UsedBW = 0;
            int Unitsprocessed;
            double sizeInBytes;

            string strSQL = null;

            LogParser = new LogQueryClassClass();
            W3Clog = new COMW3CInputContextClassClass();

            try
            {
                //W3C Logparsing SQL. Replace this SQL query with whatever 
                //you want to retrieve. The example below 
                //will sum up all the bandwidth
                //Usage of a specific folder with name 
                //"userID". Download Log Parser 2.2 
                //from Microsoft and see sample queries.

                strSQL = //the query (i changed it during test)

                // run the query against W3C log
                rsLP = LogParser.Execute(strSQL, W3Clog);

                rowLP = rsLP.getRecord();

                Unitsprocessed = rsLP.inputUnitsProcessed;

                if (rowLP.getValue(0).ToString() == "0" ||
                    rowLP.getValue(0).ToString() == "")
                {
                    //Return 0 if an err occured
                    UsedBW = 0;
                    return UsedBW;
                }

                //Bytes to MB Conversion
                double Bytes = Convert.ToDouble(rowLP.getValue(0).ToString());
                UsedBW = Bytes / (1024 * 1024);

我找到的最好的查询是这个,它在注册表中搜索(我没有原始查询,因为我改变了它

ILogRecordset rs = null;
            try
            {
                LogQueryClass qry = new LogQueryClass();
                COMRegistryInputContextClass registryFormat = new COMRegistryInputContextClass();
                string query = //Chnaged it
                rs = qry.Execute(query, registryFormat);
                for (; !rs.atEnd(); rs.moveNext())
                    Console.WriteLine(rs.getRecord().toNativeString(","));
            }
            finally
            {
                rs.close();
            }

还有一些东西,似乎将数据返回给 SQL,而不是作为活动对象:|

由于我要编写服务,我希望尽可能少地访问数据库以交换数据,只存储当前数据

--编辑:

让我补充一下,我的查询(使用传递“log parser.exe”参数)是这样的,但是在服务内部调用 exe 文件对我来说太粗鲁了。

myProc.StartInfo.Arguments = " \"select * INTO AuditLogParser from \\\\" + Dns.GetHostName() +
                                         "\\security WHERE TimeGenerated >= '" + startDate +
                                         "' and TimeGenerated <'" + endDate +
                                         "' and (eventid=560 or eventid=540)\" -o:SQL -server:\"" +
                                         Dns.GetHostName() +
                                         "\\sqlexpress\"  -database:SecurityLog -driver:\"SQL Server\" -username:TrainAudit -password:Password.110 -createtable:OFF";

【问题讨论】:

  • 评论没有容量,它不允许我在 8 小时内发布答案:|
  • 我执行编辑,检查主帖

标签: c# logparser


【解决方案1】:

以下示例代码使用 LogParser 从名为 C:\Bat 的本地目录中读取文件名和大小

LogQueryClass logQuery = new LogQueryClassClass();

COMFileSystemInputContextClass inputFormat = new COMFileSystemInputContextClassClass();
inputFormat.recurse = 0;

String strQuery = @"SELECT Name, Size FROM 'C:\Bat\*.*' ORDER BY Name ASC";

ILogRecordset results = logQuery.Execute(strQuery, inputFormat);
while (! results.atEnd())
{
    Console.WriteLine(String.Format("File: {0} Size: {1}", results.getRecord().getValue("Name"), results.getRecord().getValue(1)));
    results.moveNext();
}

请注意,我可以通过查询中的列名或列索引来访问该列。

【讨论】:

  • 顺便说一句,你不认为解析成文件很粗鲁吗:|我想要的不是将数据存储到文件中,我想要像我的第三个示例一样的东西,以这一行开头:ILogRecordset rs = null;但作为您的示例,使用索引输出或类实例输出...
  • 嗯?这不会解析为文件。上面的代码得到一个记录集并循环它。你还想要什么?
  • 啊哈,对不起,它是从文件中读取的:D 哇,顺便说一句,这个日志解析器非常强大,我看到用户到处都在使用它:-s
  • 伙计,顺便说一句,那是 COMFileSystemInputContextClass,我想通过系统事件表查询,这是安全的,现在我应该使用什么类?我通过 logparser2.2.exe 作为参数的查询是:" \"select * INTO AuditLogParser from \\\\" + Dns.GetHostName() +"\\security WHERE TimeGenerated >= '" + startDate +"' 和 TimeGenerated
  • 我猜COMEventLogInputContextClass和COMSYSLOGOutputContextClass分别。
猜你喜欢
  • 2021-09-17
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多