很久以前,我编写了仍在使用的Web服务。现在,我计划对其进行重构。 Web服务充满了最有可能未使用的功能,我不知道客户端如何使用它。为了剥离未使用的功能,我需要分析功能调用和当前安装的Web服务的数据。
是否有一个(免费/开源)工具可以使我记录Web服务的所有活动。
我要寻找的工具的理想输出可能是包含所有被调用函数的数据库,以及每个调用发送给它的数据列表。
解
借助Martins的答案,我创建了这个HttpModule,它确实实现了我想要的功能:
public class LoggingModule : IHttpModule
{
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
private void BeginRequest(object sender, EventArgs e)
{
TryAppendLog("Content-Type");
TryAppendLog("SOAPAction");
}
void TryAppendLog(string key)
{
string value = HttpContext.Current.Request.Headers[key];
if (string.IsNullOrEmpty(value)) { return; }
HttpContext.Current.Response
.AppendToLog(string.Format("{0}: {1} ", key, value));
}
#region IHttpModule Member
public void Dispose() { }
#endregion
}
最佳答案
正如Kobi所写,您可以在IIS日志文件中找到所需的信息(即在c:\ WINDOWS \ system32 \ LogFiles \ W3SVC1中)。
如果要将使用情况记录到数据库中,则可以编写一个简单的HttpModule,它检查每个请求,如果是对Web服务的调用,则将其记录到DB中。
例如。这是一个非常简单的HttpModule的相关部分,该模块将调用记录到mywebservice.asmx:
public class MyWebServiceDiagnosticsModule : IHttpModule
{
public MyWebServiceDiagnosticsModule ()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
private void BeginRequest(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
string url = ctx.Request.Url.ToString().ToLower();
if (url.Contains("mywebservice.asmx"))
{
LogMethodCall(url); // parse URL and write to DB
}
}
}