我们曾经在ASP.NET上使用WCF,最近通过ASP.NET Core切换到WCF。这很难实现,因为ASP.Net Core不支持开箱即用的WCF。首先,整个web.config XML配置模型已被转储到ASP.NET Core中,因此我们无法在那里配置WCF跟踪。
即这个文件没用:https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing
我们不得不通过在WCF和端口80之间放置一个http代理来破解ASP.NET Core.WCF实际上是在另一个端口上运行。
问题是,如果ASP.NET Core不关注web.config,我们如何启用WCF跟踪?
您将在.NET Core上使用ETW Tracing for WCF
https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md
根据我的经验,你有一些限制
ETW的好处
在客户端跟踪的情况下,我使用自定义消息日志检查器(IEndpointBehavior
)的自定义端点行为(IClientMessageInspector
)来获取输入和输出消息。
客户初始化:
_serviceClient = new MyCustomServiceClient();
_serviceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(_configParams.ServiceUri);
_serviceClient.Endpoint.EndpointBehaviors.Add(new EndpointLoggingBehavior("MyCustomService"));
EndpointLoggingBehavior
的实施:
public class EndpointLoggingBehavior : IEndpointBehavior
{
public EndpointLoggingBehavior(string serviceName)
{
_serviceName = serviceName;
}
private readonly string _serviceName;
public void AddBindingParameters(ServiceEndpoint endpoint,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new MessageLoggingInspector(_serviceName));
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
MessageLoggingInspector
的实施:
public class MessageLoggingInspector : IClientMessageInspector
{
private readonly string _serviceName;
public MessageLoggingInspector(string serviceName)
{
_serviceName = serviceName;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
// copying message to buffer to avoid accidental corruption
var buffer = reply.CreateBufferedCopy(int.MaxValue);
reply = buffer.CreateMessage();
// creating copy
var copy = buffer.CreateMessage();
//getting full input message
var fullInputMessage = copy.ToString();
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// copying message to buffer to avoid accidental corruption
var buffer = request.CreateBufferedCopy(int.MaxValue);
request = buffer.CreateMessage();
// creating copy
var copy = buffer.CreateMessage();
//getting full output message
var fullOutputMessage = copy.ToString();
return null;
}
}
然后,当然,您需要将这些消息写入任何存储。