有没有办法查看谁使用我的网络服务以及他如何使用它?那有记录系统吗?
我的消费者不确定他是否已连接到我的网络服务,我也看不到。
答案 0 :(得分:5)
您可以使用log4net等记录库或enterprise library
中的记录库如果您的服务是WCF服务,则添加操作行为允许您在每次调用操作时执行某些操作
internal class OperationLoggerBehavior : IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(new OperationLogger());
}
public void Validate(OperationDescription operationDescription)
{
}
}
internal class OperationLoggerAttribute : Attribute, IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
foreach (OperationDescription operationDescription in contractDescription.Operations)
{
if (!operationDescription.Behaviors.Contains(typeof(OperationLoggerBehavior)))
{
operationDescription.Behaviors.Add(new OperationLoggerBehavior());
}
}
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
}
}
internal class OperationLogger : IParameterInspector
{
/// <summary>
/// Called before an operation is invoked.
/// </summary>
/// <param name="operationName"></param>
/// <param name="inputs"></param>
/// <returns></returns>
public object BeforeCall(string operationName, object[] inputs)
{
// Write to log
}
/// <summary>
/// Called after an operation has been invoked.
/// </summary>
/// <param name="operationName"></param>
/// <param name="outputs"></param>
/// <param name="returnValue"></param>
/// <param name="correlationState"></param>
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
// Write to log
}
}
// Service contract implementation
[OperationLogger]
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public partial class MyService : IMyService
{
...
}
答案 1 :(得分:1)