统计WebService的调用者、调用函数、运行时间

前端之家收集整理的这篇文章主要介绍了统计WebService的调用者、调用函数、运行时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

系统WebServic分布太久了, 都不知道哪些系统在用? 调用的哪些函数?于是乎,写一个Soap Extension, 再加一个页面,来查询一下。

先看一下配制说明,和效果

web.config中

<system.web>
     <compilation debug= "true"  targetFramework= "4.0"  />
   <!--<httpHandlers>
<add path= "dl"  type= "OA4.SOA.Impl.HttpHandler.DownloadAttach"  verb= "GET" />
</httpHandlers>-->
<webServices>
<soapExtensionTypes>
       <add type= " OA4.CommonLib.Soap.TimeWatchExtension,OACommonLib"
           priority= "1"
           group = "0"  />
</soapExtensionTypes>
</webServices>
 
@H_404_106@ </system.web>

  

然后访问查询页面

 

当前运行:0
 
最后记录:20,    3.0 (Call/S),27.8 (MS/Call)
Host:10.129.255.105,  UseTime:0,Time:2012-9-4 9:15:42,name:GetCanStarFlowList,Arg:AComp:=衡水分公司,ADept:=县公司,AUser:=杨立华
Host:10.129.255.104,  UseTime:46.8747,Time:2012-9-4 9:15:43,name:GetBillData_done,Arg:ABillID:=54533f29-8979-4b1d-adf8-b8fbf2cf7678,year:=
Host:10.129.255.218,Arg:sUserName:=耿书芬,sCompany:=邯郸分公司,sDepartment:=广平分公司,sDuty:=,order:=DESC,pageSize:=20,recordCount:=0
Host:10.129.255.216,Time:2012-9-4 9:15:45,Arg:sUserName:=魏广芹,sCompany:=张家口分公司,sDepartment:=渠道管理中心,recordCount:=0
404_106@ 403f-ba4b-485c72cbb96e,sFlowInstanceID:=e14d1f34-53b5-4824-afd9-f8207dc4bab3,sActivityID:=c16d997c-f471-49f1-adef-e64cad616d79
 

实现代码:Soap

using  System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Web.Services.Protocols;
System.Web;
System.Collections.Specialized;
System.Collections.Concurrent;
System.Collections;
log4net;
System.Threading;
 
namespace  OA4.CommonLib.Soap
@H_404_106@ {
     public  class  TimeWatchExtension : SoapExtension
{
         protected  static  ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 
private  ConcurrentDictionary<Guid,WSInvokeInfo> running = new  System.Collections.Concurrent.ConcurrentDictionary<Guid,WSInvokeInfo>();
 
ConcurrentQueue<WSInvokeInfo> last = ConcurrentQueue<WSInvokeInfo>();
 
ConcurrentDictionary< string ,ConcurrentQueue<WSInvokeInfo>> remoteUserHost =  
 
get  { return  running; } }
 
ConcurrentQueue<WSInvokeInfo> LastInvoke { last; } }
 
RemoteHostInfo[] RemoteUserHost { remoteUserHost.ToList().ConvertAll(d=> RemoteHostInfo(){ Host = d.Key,LastInvoke = d.Value.ToArray()}).ToArray(); } }
 
static  int  MaxRunningMilliseconds = int .Parse(System.Configuration.ConfigurationManager.AppSettings[ "TimeWatchExtension.MaxRunningMilliseconds" ] ?? "800" );
 
Timer timerSnap = null ;
TimeWatchExtension()
         {
             timerSnap = Timer( TimerCallback(e => {
 
                 if  (Running.Count > 0)
                 {
                     var  running = Running.ToList();
 
                     running.ForEach(d => {
                         (d.Value.UseTime.TotalMilliseconds > MaxRunningMilliseconds)
                         {
                             log.Warn(d.Value.ToString());
}
});
}
             
}));
 
timerSnap.Change(1000,1000);
}
 
 
 
private  WSInvokeInfo invokeInfo = WSInvokeInfo();
override  System.IO.Stream ChainStream(System.IO.Stream stream)
{
             stream;
}
 
override  object  GetInitializer(Type serviceType)
{
return  ;
}
 
GetInitializer(LogicalMethodInfo methodInfo,SoapExtensionAttribute attribute)
{
;
}
 
void  Initialize( initializer)
{
 
}
 
 
ProcessMessage(SoapMessage message)
{
(message is  SoapClientMessage)
{
switch  (message.Stage)
{
case  SoapMessageStage.BeforeSerialize:
 
break ;
 
 
SoapMessageStage.AfterSerialize:
;
 
 
SoapMessageStage.BeforeDeserialize:
;
 
                     // About to call methods
SoapMessageStage.AfterDeserialize:
;
 
// After Method call
default :
throw  Exception( "No stage such as this" );
}
 
}
else  SoapServerMessage)
{
SoapServerMessage msg = (SoapServerMessage)message;
(message.Stage)
{
SoapMessageStage.BeforeDeserialize:
;
 
SoapMessageStage.AfterDeserialize:
{
                             //采集时间
                             this .invokeInfo.BeginInvokeTime = DateTime.Now;
//采集WebService方法
.invokeInfo.MethodName = message.MethodInfo.Name;
 
.invokeInfo.UserHostAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
 
 
.invokeInfo.Args = Dictionary< object >();
 
message.MethodInfo.InParameters.ToList().ForEach(d =>
                                 {
                                     .invokeInfo.Args.Add(d.Name,message.GetInParameterValue(d.Position));
});
 
running.TryAdd( .invokeInfo.Id,monospace!important; font-size:12px!important; min-height:auto!important; background:none!important">.invokeInfo);
 
{
last.Enqueue(invokeInfo);
 
                                 (last.Count > 20)
{
                                     WSInvokeInfo removed;
last.TryDequeue( out  removed);
}
}
{
queue = remoteUserHost.GetOrAdd(invokeInfo.UserHostAddress,uha => ConcurrentQueue<WSInvokeInfo>());
queue.Enqueue(invokeInfo);
(queue.Count > 5)
{
WSInvokeInfo removed;
queue.TryDequeue( removed);
}
}
}
;
 
SoapMessageStage.BeforeSerialize:
{
//采集时间
.invokeInfo.EndInvokeTime = DateTime.Now;
WSInvokeInfo removed;
running.TryRemove( removed);
 
(log.IsDebugEnabled)
{
( .invokeInfo.UseTime.TotalMilliseconds > MaxRunningMilliseconds)
log.Debug( .invokeInfo.ToString());
}
}
;
 
SoapMessageStage.AfterSerialize:
;
 
:
);
}
 
}
}
 
 
}
 
WSInvokeInfo
{
public  WSInvokeInfo()
{
Id = Guid.NewGuid();
}
Guid Id { get ; set ; }
DateTime BeginInvokeTime { ; }
 
string  MethodName { ; }
 
UserHostAddress { ; }
 
 
DateTime? EndInvokeTime { ; }
 
> Args { ; }
 
TimeSpan UseTime { (EndInvokeTime.HasValue ? EndInvokeTime.Value : DateTime.Now) - BeginInvokeTime; } }
 
GetString(ICollection val)
{
ret = List< >();
iter = val.GetEnumerator();
while  (iter.MoveNext())
{
(iter.Current ICollection)
ret.Add(GetString((ICollection)iter.Current));
else
ret.Add(iter.Current.ToString());
}
 
.Concat( "[" .Join( "," "]" );
}
 
 
ToString()
{
.Format( "Host:{0},  UseTime:{1},Time:{2},name:{3},Arg:{4}" .UserHostAddress ?? "none" .UseTime.TotalMilliseconds,
.BeginInvokeTime,
.MethodName ?? "unkown" .Args.ToList().ConvertAll(d => "{0}:={1}" null  ? "null"  : (d.Value ICollection ? GetString((ICollection)d.Value) : d.Value.ToString()))).ToArray()));
}
 
}
 
RemoteHostInfo
{
Host { ; }
 
WSInvokeInfo[] LastInvoke { ; }
}
 
}

查看页面

<%@ WebHandler Language= "C#"  Class= "SoapUtil.ServerStat"  %>
System;
System.Collections.Generic;
System.Linq;
System.Web;
OA4.CommonLib.Soap;
 
SoapUtil
{
     /// <summary>
/// Alive 的摘要说明
/// </summary>
ServerStat : IHttpHandler
@H_404_106@  
ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain" ;
resp = context.Response;
try
{
running = TimeWatchExtension.Running.ToList();
 
resp.Write( "当前运行:{0}\n{1}"                                          running.Count,
                                         "\n" {
                                             ret = "exception" ;
try  { ret = d.Value.ToString(); }
catch  (Exception ex)
                                             {
                                                 .Concat(ret,d.Value.MethodName,d.Value.UserHostAddress,d.Value.BeginInvokeTime.ToString());
}
ret;
}).ToArray())
)
                            );
 
last = TimeWatchExtension.LastInvoke.ToList();
{
strInfo = "" ;
(last.Count > 1)
{
f = last.First();
TimeSpan ts = DateTime.Now - f.BeginInvokeTime;
speed = last.Count / ts.TotalSeconds;
avgUseTime = last.Average(d => d.UseTime.Milliseconds);
 
 
strInfo = "{0:0.0} (Call/S),{1:0.0} (MS/Call)" }
else
{
 
}
"\n最后记录:{0},\t{1}\n{2}"                                             last.Count,
strInfo,
                                            {
                                                ;
{ ret = d.ToString(); }
(Exception ex)
                                                {
                                                    }
ret;
}).ToArray())
)
                               );
}
 
resp.Write( );
userHost = TimeWatchExtension.RemoteUserHost;
Array.ForEach(userHost,info =>
{
invoke = info.LastInvoke.ToList();
(invoke.Count > 1)
f = invoke.First();
 
TimeSpan ts = DateTime.Now - f.BeginInvokeTime;
speed = invoke.Count / ts.TotalSeconds;
avgUseTime = invoke.Average(d => d.UseTime.Milliseconds);
 
 
"\n\n"  + info.Host + "["  + strInfo + "]:" );
}
else
{
":" );
}
"\n最后记录:{0}\n{1}"                                        invoke.Count,
                                       {
                                           { ret = d.ToString(); }
(Exception ex)
                                           {
                                               }
ret;
}).ToArray())
)
                          );
});
 
}
(Exception ex)
{
context.Response.Write( "ERROR:{0}" }
}
 
bool  IsReusable
{
get
{
false ;
}
}
}
}

猜你在找的WebService相关文章