加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 21:42:28 所属栏目:安全 来源:网络整理
导读:系统WebServic分布太久了, 都不知道哪些系统在用? 调用的哪些函数?于是乎,写一个Soap Extension, 再加一个页面,来查询一下。 先看一下配制说明,和效果 web.config中 system.web ???? compilation debug= "true" ? targetFramework= "4.0" ? / ?? !--h

系统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>
?
</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
?

实现代码: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
{
???? 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
?
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);
?
?
"nn"? + 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 ;
}
}
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读