Udostępnij za pośrednictwem


So you want to replay an IIS web server log?

A few months ago, a group in Microsoft wanted to be able to play back a large IIS log as a Visual Studio web test.  They started off with a converter that converted the IIS log into a gigantic coded web test.  The 118MB .cs file that resulted was a bit ridiculous and didn't perform very well at design time or run time.

I took a different approach by reading the IIS log from within the web test.  It depends on the the handy LogReader 2.2 download to handle all the log parsing and keep the code short and simple.

Here's a sample WebTest that plays back an IIS log:

public class IISLogCodedWebTest : WebTest
{
public IISLogCodedWebTest()
{
this.PreAuthenticate = true;
}

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
IISLogReader reader = new IISLogReader(@"d:\download\ex060209.log");
foreach (WebTestRequest request in reader.GetRequests())
{
yield return request;
}
}
}

The code for the IISLogReader class used above is below:

using System;
using System.Collections.Generic;
using System.Text;
using MSUtil;
using LogQuery = MSUtil.LogQueryClassClass;
using IISLogInputFormat = MSUtil.COMIISW3CInputContextClassClass;
using LogRecordSet = MSUtil.ILogRecordset;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace IISLogToWebTest
{
public class IISLogReader
{
private string _iisLogPath;

        public IISLogReader(string iisLogPath)
{
_iisLogPath = iisLogPath;
}

        public IEnumerable<WebTestRequest> GetRequests()
{
LogQuery logQuery = new LogQuery();
IISLogInputFormat iisInputFormat = new IISLogInputFormat();

            string query = @"SELECT s-ip, s-port, cs-method, cs-uri-stem, cs-uri-query FROM " + _iisLogPath;

            LogRecordSet recordSet = logQuery.Execute(query, iisInputFormat);
while (!recordSet.atEnd())
{
ILogRecord record = recordSet.getRecord();
if (record.getValueEx("cs-method").ToString() == "GET")
{
string server = record.getValueEx("s-ip").ToString();
string path = record.getValueEx("cs-uri-stem").ToString();
string querystring = record.getValueEx("cs-uri-query").ToString();

                    StringBuilder urlBuilder = new StringBuilder();
urlBuilder.Append("https://");
urlBuilder.Append(server);
urlBuilder.Append(path);
if (!String.IsNullOrEmpty(querystring))
{
urlBuilder.Append("?");
urlBuilder.Append(querystring);
}

                    WebTestRequest request = new WebTestRequest(urlBuilder.ToString());
yield return request;
}

                recordSet.moveNext();
}

recordSet.close();
}
}
}

Let me know if you find this useful or if you have any problems.

Comments

  • Anonymous
    July 04, 2006
    Josh Christie shares some code to replay an IIS Log. Cool.
    Technorati : Josh Christie; IIS; Replay.

  • Anonymous
    September 07, 2006
    &amp;lt;In case you are wondering what these blog posts are about, these are some questions I got from recent

  • Anonymous
    May 22, 2007
    You've written your web application and tested it to ensure it all works but how do you know it will

  • Anonymous
    March 17, 2008
    Visual Studio Team System for Testers Content Index for Web Tests and Load Tests Getting Started Online

  • Anonymous
    June 03, 2008
    I had a few questions in Denver at the Adopting VSTS 2008 event – I took some of them offline due to...