C# 一般计算WCF操作执行时间

C# 一般计算WCF操作执行时间,c#,.net,wcf,generics,C#,.net,Wcf,Generics,使用.NET 3.5 C#,我在IIS中托管了一个WCF web服务,其中包含多个ServiceContract,每个服务都有多个OperationContract。最近,我看到了性能问题,并且希望一般地记录每个操作的执行时间,而不必向每个方法添加代码。实现这一目标的最佳方式是什么。以下是我的一个服务合同界面: [ServiceContract] public interface IAuthentication { [OperationContract] [Web

使用.NET 3.5 C#,我在IIS中托管了一个WCF web服务,其中包含多个ServiceContract,每个服务都有多个OperationContract。最近,我看到了性能问题,并且希望一般地记录每个操作的执行时间,而不必向每个方法添加代码。实现这一目标的最佳方式是什么。以下是我的一个服务合同界面:

[ServiceContract]
public interface IAuthentication
{        
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "AuthBegin", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply AuthBegin(AuthBeginRequest authBegin);

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "AuthComplete", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply AuthComplete(AuthCompleteRequest authComplete);

    [OperationContract]
    [WebGet(UriTemplate = "Logout", ResponseFormat = WebMessageFormat.Json)]
    ServiceReply LogOut();

}
下面是一个示例实现,我目前在操作中放置了秒表,我想对其进行更改:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]   
public class Authentication : IAuthentication
{              
    public ServiceReply Auth1(ClientConfig _clientConfig)
    {            
        ServiceReply ret = new ServiceReply();
        Stopwatch sw = Stopwatch.StartNew();                        

        try
        {

        }
        catch (Exception exc)
        {

        }

        sw.Stop();
        //Log.InfoFormat("Method {0} --> Elapsed time={1}", methodName, sw.Elapsed);

        return ret;
    }
}
此接口用于调用您的服务方法。您可以使用秒表和异常处理来包装您的服务方法。您也可以在那里写入日志项

有一种将调用程序应用于所有服务方法的中心方法。参见链接问题