fsp*_*rit 24
我会实现自定义IErrorHandler并使用log4net
[AttributeUsage (AttributeTargets.Interface)]
public class ErrorPolicyBehaviorAttribute : Attribute, IContractBehavior, IErrorHandler
{
private ILog m_logger;
#region IErrorHandler
public void ProvideFault (Exception error, MessageVersion version, ref Message fault)
{
return;
}
public bool HandleError (Exception error)
{
m_logger.Error (error.Message, error);
return true;
}
#endregion
#region IContractBehavior
public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
...init logger
......Add this class to a list of dispatchRuntime.ChannelDispatcher.ErrorHandlers...
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
此类还实现了IContractBehavior,因此您可以将其用作服务契约的Attribute.
[ErrorPolicyBehavior]
public interface IYourServiceContract
{ }
Run Code Online (Sandbox Code Playgroud)
log4net非常灵活,因此您可以根据需要记录所需内容.
Dan*_*llo 10
可以将WCF配置为跨应用程序的所有组件输出流程里程碑的跟踪,例如操作调用,代码异常,警告和其他重要的处理事件.
以下是app.config启用跟踪的示例.
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource" switchValue="Warning, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="TraceLog.svclog" />
</sharedListeners>
</system.diagnostics>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Microsoft提供了一个服务跟踪查看器工具来读取.svclog文件.
除了跟踪之外,您可能还需要考虑使用log4net进行应用程序内日志记录.