WCF服务器端日志消息发送回进度

时间:2016-06-15 09:45:49

标签: wcf

我在客户端获得超时,我不确定问题是包大小还是网络。

在服务器端是否有记录发送到客户端的消息进度的方法? 或者至少是传输层上发送的包的大小?

注意:我在发布问题之前已经查看了WCF日志记录选项,但无法找到有关邮件大小的任何内容,特别是如果绑定不是Http,那么没有具有Content-Length的特定标头。

由于

2 个答案:

答案 0 :(得分:0)

您可以在配置文件中添加以下行:

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
            <listeners>
                <add name="ServiceModelTraceListener" initializeData="c:\MyTracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" traceOutputOptions="Timestamp"/>
            </listeners>
        </source>
    </sources>
    <trace autoflush="true" />
</system.diagnostics>
<system.serviceModel>
    <diagnostics>
        <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
</system.serviceModel>

您可以在MSDN页面找到更多信息。

答案 1 :(得分:0)

这是我设法实现的最好的事情,无法找到任何开箱即用的东西:

public class MessageSizeBehaviorAttribute : Attribute, IContractBehavior
{
    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.MessageInspectors.Add(new MessageSizeInspector());
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters)
    {
    }
}

public class MessageSizeInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        return request;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        try
        {
            if (reply.IsFault || reply.IsEmpty) return;

            var buffer = reply.CreateBufferedCopy(int.MaxValue);
            reply = buffer.CreateMessage();
            var messageBodyReader = buffer.CreateMessage().GetReaderAtBodyContents();
            var messageBody = messageBodyReader.ReadOuterXml();

            LogMessageSize(messageBody);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }

    private void LogMessageSize(string messageBody)
    {
        double messageSize = Encoding.UTF8.GetByteCount(messageBody);
        Debug.WriteLine("Message size in MBytes {0}", messageSize / 1024 / 1024);
    }
}