Execute log parser using C #

Source: Internet
Author: User
Com through. NET Framework InterOP(COM Interactive Operation) features, can be easily applied in. netProgramUsing Log parser,. NET Framework com InterOPIs implemented through runtime callable wrappers (RCW) for com operations, RCW is a class in. net.

Compiling environment: vs2005 C #

First, add reference: logparser. DLL to your new project, and then it appears in the reference bar of your solution: msutil. You can find various types of interfaces in the library in the Object Browser. Next, reference the type you want to call at the beginning of your CS file, for example, using logquery = msutil. logqueryclassclass; using logrecordset = msutil. ilogrecordset; using logrecord = msutil. ilogrecord; using w3cloginputformat = msutil. comw3cinputcontextclassclass; using csvlogoutputformat = msutil. comcsvoutputcontextclassclass;
For example, use Log parser and C # to create a website access statistical analysis system. Use C # To call log parser and use Log parser to analyze IIS log files. Here, the log file is in W3C format.

Create a. CS file and introduce:

Using logquery = InterOP. msutil. logqueryclassclass;
Using iisinputformat = InterOP. msutil. comiisw3cinputcontextclassclass;
Using logrecordset = InterOP. msutil. ilogrecordset;

Class keyCodeAs follows:

Logquery ologquery = new logquery ();
Iisinputformat oiisinputformat = new iisinputformat ();
String query = @ "select count (distinct C-IP) as hits
From 'C: \ windows \ system32 \ logfiles \ w3svc1 \ ex07081_log'
Where CS-Uri-stem like '%. asp'
And SC-status = 200 ";
Logrecordset orecordset = ologquery. Execute (query, oiisinputformat );
If (! Orecordset. atend ())
{
Hits = (INT) orecordset. getrecord (). getvalue ("hits ");
}
Orecordset. Close ();

It can be seen that it is not much different from using the database. In the query statement, C-IP, CS-Uri-stem, SC-status, and so on are all log file fields, you can directly open the log file to find the content.

Through the combination of C # And logparser, You can maximize the functions of logparsr. So that it is not limited to command line input...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.