Dan Thomas
Dan Thomas

Reputation: 831

IDispatchMessageInspector to log plain-text soap messages?

I've written a WCF IDispatchMessageInspector, so I can log incomming and outgoing messages. But I'm not sure how to get a nicely-formatted XML string to log.

My code looks something like this:

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        Log("Received", request.ToString();
        return null;
    }

The result of this logging includes things like "ampersand lt;" and some sort of binary-encoded data.

How do I get something that looks like a standard soap XML document? I know this should be obvious, but I'm just too dense to figure it out.

Thanks.

Dan

Upvotes: 2

Views: 1694

Answers (2)

TomB
TomB

Reputation: 765

Here is a working example

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
    request = buffer.CreateMessage();

    XDocument doc;

    using (MemoryStream ms = new MemoryStream())
    {
        XmlWriter writer = XmlWriter.Create(ms);
        request.WriteMessage(writer);
        writer.Flush();
        ms.Position = 0;

        doc = XDocument.Load(ms);
    }

    if (SaveLog != null)
    {
        LogSaveFileEventArgs logEventArgs = new LogSaveFileEventArgs(doc, false);
        SaveLog(this, logEventArgs);
    }

    request = buffer.CreateMessage();

    //The return value can be any object that you want to use for correlation purposes;
    //it is returned to you as the correlationState parameter in the BeforeSendReply method.
    return null;
}

Upvotes: 1

Marvin Smit
Marvin Smit

Reputation: 4108

Is there any specific reason you are not using the .Net build in tracing functionality with an XmlTextWriter output?

If no specific reason, have a look at http://msdn.microsoft.com/en-us/library/ms730064.aspx

Hope this helps,

Upvotes: 1

Related Questions