로깅 용으로 하나 이상의 서비스를 만드는 다른 방법이 있습니다. 애스펙트로 로깅을 구축하고 필요에 따라 언제든지 ServiceContract
또는 OperationContract
으로이 애스펙트 (일명 인젝션)를 첨부/분리 할 수 있습니다. 이렇게하면 로깅을 분리 할 수 있지만 모든 호출에서 하나 더 많은 서비스를 호출하는 오버 헤드를 피할 수 있습니다. 이러한 측면을 작성하고 별도의 바이너리로 컴파일 한 후 나중에 필요할 때마다 필요할 때 사용할 수 있으므로 특정 로깅 시나리오를 활성화 및 비활성화하는 것은 단순한 로깅을위한 전용 서비스를 사용하는 것과 비교하여 훨씬 유지 보수가 간편합니다.
다음 두 개의 게시물을 살펴보고 간단한 방법으로이 작업을 수행하면 프로젝트에 원하는대로 육체를 작성해야합니다. Creating a logging Aspect 당신이보고 싶어
WCF Extensibility: Parameter Inspectors
중요 MSDN 문서
- .
편집 IParameterInspector
- IOperationBehavior
- 코드 아래의 샘플 코드
당신이 당신의 작업 계약의 위 [OperationLogging]
을 추가하고 호출을 가로 챌 수 있습니다 이 작업 계약은 LoggingInspector.BeforeCall
입니다.
서비스 계약에 [ServiceLogging]
을 사용하면 해당 서비스 호출에 정의 된 모든 작업이 가로 채고 기록 될 수 있습니다.
을 TRUE
이외의 것으로 설정하면 이러한 추가 동작이 서비스 파이프 라인에 추가되지 않습니다. 이 코드는 config의이 키를 기반으로 실행되지 않으므로 매우 멋지다.
public class LoggingInspector : IParameterInspector
{
private string service;
public LoggingInspector(string serviceName){ service = serviceName;}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState){}
public object BeforeCall(string operationName, object[] inputs)
{
// your logging logic
}
}
//Operation Logging attribute - applied to operationcontracts.
[AttributeUsage(AttributeTargets.Method)]
public class OperationLoggingAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters){}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation){}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
if (ConfigurationManager.AppSettings["your_app_config_key"] == "TRUE")
dispatchOperation.ParameterInspectors.Add(new LoggingInspector(dispatchOperation.Parent.Type.Name));
}
public void Validate(OperationDescription operationDescription){}
}
//Service Loggign attribute - applied to Service contract
[AttributeUsage(AttributeTargets.Class)]
public class ServiceLoggingAttribute : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters){}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
if (ConfigurationManager.AppSettings["your_app_config_key"] == "TRUE")
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
foreach (OperationDescription operation in endpoint.Contract.Operations)
operation.Behaviors.Add(new OperationLoggingAttribute());
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase){}
}
WCF를 사용하면 같은 서비스 (net.tcp를 사용하는 것만이 아니라)에서 여러 끝점 (여러 프로토콜을 통해)을 노출 할 수도 있습니다. –
WCF가 좋은 선택입니다! 시애틀에서 미네아폴리스까지의 비행기에서 나는 코드 우선 EF4 데이터 레이어가있는 WCF 서비스를 작성했습니다. 그것은 내가 기대했던 것보다 매우 우아하고 더 공연적입니다. 로그 메시지를 보내려는 작은 클라이언트를 만들었고 각각의 로그 메시지는 우리가 작성하는 로그 메시지보다 큽니다. 매우 흥미로운. –