Introduction
The very first thing I did was to find out if there is any Micosoft component which can help me in parsing IIS log and when I googled it I found logparser.com (
Click here to view) . and as per microsoft Log parser is a powerful, versatile tool that provides universal query access to text-based data such as log files, XML files and CSV files, as well as key data sources on the Windows® operating system such as the Event Log, the Registry, the file system, and Active Directory®.
You tell Log Parser what information you need and how you want it processed. The results of your query can be custom-formatted in text based output, or they can be persisted to more specialty targets like SQL, SYSLOG, or a chart.
An IIS log can contains following fields
Date:- The date on which the activity occurred.
Time :- The time, in coordinated universal time (UTC), at which the activity occurred.
Client IP Address:-The IP address of the client that made the request.
User Name :-The name of the authenticated user who accessed your server. Anonymous users are indicated by a hyphen.
Service Name and Instance Number:- The Internet service name and instance number that was running on the client.
Server Name:- The name of the server on which the log file entry was generated.
Server IP Address:- The IP address of the server on which the log file entry was generated.
Server Port :- The server port number that is configured for the service.
Method :-The requested action, for example, a GET method.
URI Stem:-The target of the action, for example, Default.htm.
URI Query:- The query, if any, that the client was trying to perform. A Universal Resource Identifier (URI) query is necessary only for dynamic pages.
HTTP Status :-The HTTP status code.
Win32 Status:- The Windows status code.
Bytes Sent:- The number of bytes that the server sent.
Bytes Received:- The number of bytes that the server received.
Time Taken:-The length of time that the action took, in milliseconds.
Protocol Version:- The protocol version —HTTP or FTP —that the client used.
Host:- The host header name, if any.
User Agent :- The browser type that the client used.
Cookie:- The content of the cookie sent or received, if any.
Referrer:- The site that the user last visited. This site provided a link to the current site.
Protocol Substatus:- The substatus error code.
You can find out more about IIS log at microsoft site (
Click here to view) but default all columns are not enabled so the one you need you have to go into IIS property and enable it,you can find out at http://blogs.iis.net on how to enable (
Click here to view)
using IIS logger I wrote following code but in order to use it first you need to down it and donot forget to add a reference to it.
using System;
using System.Collections.Generic;
using System.Text;
using MSUtil;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
namespace IISLogParser{
class Program{
static void Main(string[] args){
ILogRecordset rsLP = null;
ILogRecord rowLP = null;
LogQueryClassClass LogParser = new LogQueryClassClass();
COMW3CInputContextClassClass W3Clog = new COMW3CInputContextClassClass();
string strSQL = null;
string IISLogPath = ConfigurationSettings.AppSettings["IISLogsPath"].ToString();
string currentPage, previousPage;
string csvFileName = ConfigurationSettings.AppSettings["CSVPath"].ToString() + System.DateTime.Now.ToString("yyyyMMddmmss") + ".csv";
StreamWriter writer = new StreamWriter(csvFileName, true);
try
{
strSQL = @"SELECT cs-uri-query, time-taken,cs-method from " + IISLogPath + @"\\*.log where cs-uri-query like 'C0=%&C3=%&C1=%' and time-taken <=1000";
rsLP = LogParser.Execute(strSQL, W3Clog);
writer.WriteLine("Query string, Time Taken,Method");
for (; !rsLP.atEnd(); rsLP.moveNext())
{
rowLP = rsLP.getRecord();
writer.WriteLine(rowLP.getValue(0)+ ","+ rowLP.getValue(1) + "," + rowLP.getValue(2) );
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
writer.Close();
writer.Dispose();
}
}
}
}
I've been workin in microsoft technologies for more then 7 years and worked on Web and windows based application.I've been working on Clasic ASP,ASP.Net,C#,Javascript,HTML,SQL server and currently exploring C#3.5 and WFC.