ASP.NET Code To Query FTP Logs Using Log Parser 2.2

I have a project where a shopping cart is using file upload web controls to allow customers to upload image files. This is not working out so well because the image files can be huge. I think the session is timing out before the file uploads and the web application may be recycling because the ASP.NET worker process exceeds its maximum memory allowance with those huge image files. Well that is my theory. I've recommended using FTP to make the file uploads less taxing on the web application. If an image file is uploaded via FTP then I will need to get some information from the FTP logs. I was unable to find any sample code for parsing FTP log files using ASP.NET.

To easiest way to parse the FTP logs would be to use the Microsoft Log Parser 2.2 which you can download at: http://www.iis.net/downloads/default.aspx?tabid=34&i=1287&g=6 When you install it there will be a compiled help file included. I converted this into a Help 2.0 Collection to integrate it with my other MSDN documentation in the Microsoft Document Explorer. Anyway, look for the C# sample code under Log Parser COM API Overview. This help topic includes information on how to use the COM interop feature of the .NET framework to instantiate and use the COM object version of LogParser. The sample code is for the System Event Log so the only value I'm adding with this blog post is to show you how to use this with the FTP logs (plus I converted the code to VB).

Note that you must change the input format to IIS W3C for the FTP logs.  My FTP logs are actually using the W3C Extended Log File Format. You don't need to know the file path for the log files but it was slightly tricky to figure out the correct syntax for the FROM part of the query. I think sample code and documentation should provide you with examples for all the options and not just a few representative cases. Don't force me to experiment with syntax! I also changed the parameter to the file last modified date. I found that my results included the logs from two log files so be aware that the query will apply to more than one log file. You should probably add the Date to your FTP log's Extended Properties so you can query by a precise date.

Imports LogQuery = MSUtil.LogQueryClassClass
Imports IISW3CLogInputFormat = MSUtil.COMIISW3CInputContextClassClass

Public Class ReadFTPLogs
    
Inherits System.Web.UI.Page

#
Region " Web Form Designer Generated Code "

    
'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    
End Sub
    Protected WithEvents lblFTPLog As System.Web.UI.WebControls.Label
    
Protected WithEvents lblErrorMessage As System.Web.UI.WebControls.Label

    
'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        
'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    
End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
Try
            ' Instantiate the LogQuery object
            Dim oLogQuery As LogQuery = New LogQuery

            
' Instantiate the W3C Log Input Format object
            Dim oIISW3CLogInputFormat As IISW3CLogInputFormat = New IISW3CLogInputFormat

            
' Set its "minDateMod" parameter
            oIISW3CLogInputFormat.minDateMod = "2007-01-01 00:00:00"

            
' Create the query
            Dim query As String = "SELECT TOP 250 time, c-ip, cs-method, cs-uri-stem, sc-status FROM <//SERVER/MSFTPSVC/1>"

            
' Execute the query
            Dim oRecordSet As MSUtil.ILogRecordset = oLogQuery.Execute(query, oIISW3CLogInputFormat)

            
' Browse the recordset
            While Not oRecordSet.atEnd()
                lblFTPLog.Text &= oRecordSet.getRecord().toNativeString(",") & "<br />"
                oRecordSet.moveNext()
            
End While

            ' Close the recordset
            oRecordSet.close()
        
Catch exc As System.Runtime.InteropServices.COMException
            lblErrorMessage.Text = "Unexpected error: " & exc.Message & "<br /><br />" & exc.StackTrace
        
End Try
    End Sub

End
Class

 

2 Comments

  • Another work around would be to set the maxRequestLength in the configuration section of your web.config to a larger value. For example:


    This would set it to 8 MB

  • Yes, I increased the maxRequestLength and the session timeout but there are still problems. Also the fancy commercial file upload web control does not allow all the acceptable image file formats.

Comments have been disabled for this content.