Using the Log Parser in an ASP.NET Web Application

I needed to query some IIS log files last week.  I wanted to break down some records into the request over a 24 hour period.  I wrote the code below.  The two special issues that I had to work through are separating out the hours through the Quantize command and then translating the resulting LogQueryRecordSet into a DataTable.  If you have any suggestions, please send them to me.


        Dim strDateStart As String = Me.calDateStart.SelectedDate.ToString("yyyy-MM-dd")
        Dim strDateEnd As String = Me.calDateEnd.SelectedDate.ToString("yyyy-MM-dd")
        Dim strSql As String = "select QUANTIZE( time, 3600 ) as qtTime, count(qtTime) as TimeCount from c:\logfil~1\ex*.log where date >= '" & strDateStart & "' and date < '" & strDateEnd & "' group by qtTime"
        Dim objMsUtil As New MSUtil.LogQueryClass
        Dim objRS As MSUtil.ILogRecordset
        Dim objRow As MSUtil.ILogRecord
        Dim dtData As New DataTable
        Dim dcData As DataColumn
        Dim drData As DataRow
        objRS = objMsUtil.Execute(strSql)
        dcData = New DataColumn("Time", System.Type.GetType("System.String"))
        dcData = New DataColumn("DownLoadCount", System.Type.GetType("System.Int32"))

        While Not (objRS.atEnd)
            drData = dtData.NewRow()
            objRow = objRS.getRecord()
            drData("Time") = Convert.ToDateTime(objRow.getValue("qtTime")).ToShortTimeString
            drData("DownLoadCount") = objRow.getValue("TimeCount")
        End While

        Me.dgSearchResults.DataSource = dtData

1 Comment

  • I know this post is quite old but I stumbled upon it as I was searching for info on the log parser. I'm sure you've moved on but I took what you did and tried to make it more generic. Basically I created a couple of functions that would take the recordset object returned from a query and convert it to a datatable so it can easily be used with a gridview or other databound control. In any event, I found your post to very helpful as it got me going in the right direction.....

    Protected Function ConvertILogRecordsetToDataTable(ByRef objRS As MSUtil.ILogRecordset) As DataTable
    Dim i As Integer
    Dim dtTemp As New DataTable
    Dim dr As DataRow
    Dim objRow As MSUtil.ILogRecord

    'Create table columns
    For i = 0 To objRS.getColumnCount - 1
    dtTemp.Columns.Add(objRS.getColumnName(i), GetDotNetType(objRS.getColumnType(i)))

    'Populate table
    While Not (objRS.atEnd)
    dr = dtTemp.NewRow()
    objRow = objRS.getRecord()

    For i = 0 To dtTemp.Columns.Count - 1
    dr(i) = objRow.getValue(i)

    End While

    ConvertILogRecordsetToDataTable = dtTemp

    End Function

    Protected Function GetDotNetType(ByVal iLogType As Integer) As Type

    Select Case iLogType
    Case 0
    GetDotNetType = GetType(Integer)
    Case 1
    GetDotNetType = GetType(DBNull)
    Case 2
    GetDotNetType = GetType(Double)
    Case 3
    GetDotNetType = GetType(String)
    Case 4
    GetDotNetType = GetType(Date)
    Case Else
    GetDotNetType = GetType(String)
    End Select

    GetDotNetType = GetType(String)

    End Function

Comments have been disabled for this content.