6

私のWindowsサービスは、1つのイベントログの内容をファイルに保存する必要があります。これは、EventLogSession.ClearLogによって実行されます。ただし、イベントログをCSVに直接保存するように強制することはできません。保存される形式はEVTXです。

            EventLogSession els = new EventLogSession();

            //stel de filename samen door het appdata pad te combinen met een tempfile name
            string tempData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "templog.csv");
            // Clears all the events and archives them to the .evtx file

            els.ClearLog(eventLogName, tempData); //  Backup File Path

EventlogSessionクラスをCSVに直接保存するように強制するにはどうすればよいですか、それが不可能な場合はどうすればよいですか。EVTXをCSVに変換するにはどうすればよいですか(C#またはVB.netを使用)

ありがとう!

4

4 に答える 4

5

これは、LogParserが提供するAPIを使用して行うのは非常に簡単です。

LogParser2.2をダウンロードしてインストールします

COMライブラリ「MSUtility1.0TypeLibrary-LogParserInterfacescollection」への参照を追加します。ログを検索すると、リストがかなり劇的に絞り込まれました。

ここに画像の説明を入力してください

相互運用タイプが埋め込まれないように、参照のプロパティを変更します。これを行わないと、次のようなコンパイルエラーが発生します。相互運用機能タイプ'MSUtil.COMCSVOutputContextClassClass'を埋め込むことはできません。代わりに、該当するインターフェースを使用してください。

MSUtil相互運用タイプ

LogParserヘルプファイルの内容にはAPIの優れたリファレンスがありますが、コードにインラインで使用した部分を含めました。

using System;
using MSUtil;

namespace LogParserTest
{
 using LogQuery = LogQueryClassClass;
 using EventLogInput = COMEventLogInputContextClassClass;
 using CSVOutput = COMCSVOutputContextClassClass;
 using XMLOutput = COMXMLOutputContextClassClass;

 class Program
 {
  static void Main(string[] args)
  {
   try
   {
    // Instantiate the LogQuery object
    LogQuery oLogQuery = new LogQuery();

    // Instantiate the Event Log Input Format object
    EventLogInput eventInputFormat = new EventLogInput();

    // When set to "FW", events are retrieved from the oldest to the 
    // newest. When set to "BW", events are retrieved from the newest 
    // to the oldest.
    eventInputFormat.direction = "FW"; 

    // Event text messages often span multiple lines. When this parameter
    // is set to "ON", the EVT input format preserves readability of the 
    // messages by removing carriage-return, line-feed, and multiple space
    // characters from the message text.
    // When this parameter is set to "OFF", the EVT input format returns
    // the original message text with no intervening post-processing. 
    eventInputFormat.formatMessage = true;

    eventInputFormat.binaryFormat = "ASC";
    eventInputFormat.stringsSep = ",";

    CSVOutput csvOutputFormat = new CSVOutput();

    // ON: always write the header; 
    // OFF: never write the header; 
    // AUTO: write the header only when not appending to an existing file. 
    csvOutputFormat.headers = "ON"; 

    // Setting this parameter to "ON" causes the CSV output format to write
    // a tab character after each comma field separator, in order to 
    // improve readability of the CSV output. Note that using tabs between
    // field values might generate output that is not compatible with 
    // certain spreadsheet applications. 
    csvOutputFormat.tabs = false;

    // ON: always enclose field values within double-quote characters; 
    // OFF: never enclose field values within double-quote characters; 
    // AUTO: enclose within double-quote characters only those field 
    //    values that contain comma (,) characters. 
    csvOutputFormat.oDQuotes = "AUTO";

    // This parameter specifies the date and/or time format to use when
    // formatting values of the TIMESTAMP data type.
    csvOutputFormat.oTsFormat = "yyyy-MM-dd";

    // 0 is the system codepage, -1 is UNICODE. 
    csvOutputFormat.oCodepage = -1;

    // 0: existing files are appended with the output; 
    // 1: existing files are overwritten with the output; 
    // 2: existing files are left intact, discarding the output. 
    csvOutputFormat.fileMode = 1;

    /*
    EventLog     STRING  Name of the Event Log or Event Log backup file 
    RecordNumber   INTEGER  Index of this event
    TimeGenerated   TIMESTAMP Event generated date/time (local time) 
    TimeWritten    TIMESTAMP Event logged date/time (local time) 
    EventID      INTEGER  The ID of the event 
    EventType     INTEGER  The numeric type of the event 
    EventTypeName   STRING  The descriptive type of the event 
    EventCategory   INTEGER  The numeric category of the event 
    EventCategoryName STRING  The descriptive category of the event 
    SourceName    STRING  The source that generated the event 
    Strings      STRING  The textual data
    ComputerName   STRING  The name of the computer  
    SID        STRING  The Security Identifier associated with the event 
    Message      STRING  The full event message 
    Data       STRING  The binary data associated with the event 
    */

    string query = @"SELECT TOP 10 EventLog, RecordNumber, Message INTO "
    // Enclose path in single ticks to handle spaces.
    query += "'" + FullPathToCsv + "' FROM "; 
    // Name of application Log, System, Security, Application, CustomLogName
    query += "System";     
    oLogQuery.ExecuteBatch(query, eventInputFormat, csvOutputFormat);
   }
   catch (System.Runtime.InteropServices.COMException ex)
   {
    Console.WriteLine("Unexpected error: " + ex.Message);
   }
  }
 }
}
于 2013-05-01T21:03:38.630 に答える
4

このPowershell関数は、私が見つけた中で最も効率的です。C#コードではありませんが、役に立つかもしれないと思いました。次のように、ファイル名(evtx)またはPowershellのファイル名の可変配列を取ります。

[配列]$filelist = "file1"、 "file2"、 "file3"

Function Convert-Logs3 {
[cmdletbinding()]
Param(
$filelist=$NULL
)
$filelist | foreach-object {
Get-WinEvent -Path "$PSItem"| Select RecordID,ID,TimeCreated, Message | export-csv - ``notypeinformation -path $(write "$PSItem.csv");
[System.gc]::collect();

}}
于 2014-02-12T18:50:40.820 に答える
2

既存のすべてのソリューションが私の要件を満たしていないことが判明しました。evtxを入力として受け取り、csvをエクスポートするツールが必要でした。それ以上でもそれ以下でもありません。自分で作成しましたが、問題なく動作します。その呼ばれるEVTX2CSV

ここからダウンロードできます:http://essaver.net/evtxecsvまたはhttp://www.essaver.net/downloads/setupevtx2csv.exeから直接ダウンロードできます。

于 2013-08-20T08:14:46.910 に答える
2

EVTXをCSVに変換するツールが必要な場合は、LogParserツールを直接使用できます。

C:\> logparser "SELECT TimeGenerated, SourceName, EventCategoryName, EventId, Message INTO C:\eventlog.csv FROM C:\eventlog.evtx" -i:EVT

これを使用して、3GBのEVTXファイルを約10分でCSVに変換することができました。

于 2014-07-23T21:06:36.267 に答える