So you want to replay an IIS web server log?

介绍了一种通过读取IIS日志文件并利用Logparser进行测试重放的方法,该方法避免了将日志转换为大型编码测试文件的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明!

陈希章

原文地址:http://www.cnblogs.com/chenxizhang/archive/2009/06/01/1493924.html
原文标题:So you want to replay an IIS web server log?
原文发表:2009/6/1 10:02:00

原文地址:http://blogs.msdn.com/joshch/archive/2006/07/03/655518.aspx

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 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 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("http://");
                    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();
        }
    }
}

 

这真是一个不错的主意:通过Logparser读取IIS日志文件,并且利用它来做测试的重放。

作者:陈希章
出处:http://blog.youkuaiyun.com/chen_xizhang
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值