Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF client logging dotnet core

I'm using asp.net core on windows and have a file with classes generated by the dotnet-svcutil. I'm using nlog for the logging purpose. Is there a way I can log all the raw requests and responses to and from the external service?

Already tried logman https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md, but first - it doesn't show the raw soap, only events, and second - I need logs to be logged by the configured nlog.

like image 390
Lanayx Avatar asked Aug 10 '16 09:08

Lanayx


People also ask

Can we use WCF in .NET Core?

Applications with WCF dependencies can be updated to use CoreWCF in-place on . NET framework, which then will work the same when updated to use . NET Core or . NET 5+.

How do I enable tracing and message logging in WCF?

Windows Communication Foundation (WCF) does not log messages by default. To activate message logging, you must add a trace listener to the System. ServiceModel. MessageLogging trace source and set attributes for the <messagelogging> element in the configuration file.

How do I enable WCF tracing on client?

Configure TracingOpen the app. config file of the WCF service library and add the configuration for system. diagnostics. In this configuration we are using "Critical, Information, ActivityTracing" as switch value.

What is logging in .NET Core?

In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).


1 Answers

Behaviour:

public class LoggingEndpointBehaviour : IEndpointBehavior
{
    public LoggingMessageInspector MessageInspector { get; }

    public LoggingEndpointBehaviour(LoggingMessageInspector messageInspector)
    {
        MessageInspector = messageInspector ?? throw new ArgumentNullException(nameof(messageInspector));
    }

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

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(MessageInspector);
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

Inspector:

 public class LoggingMessageInspector : IClientMessageInspector
{
    public LoggingMessageInspector(ILogger<LoggingMessageInspector> logger)
    {
        Logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
    }

    public ILogger<LoggingMessageInspector> Logger { get; }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        using (var buffer = reply.CreateBufferedCopy(int.MaxValue))
        {
            var document = GetDocument(buffer.CreateMessage());
            Logger.LogTrace(document.OuterXml);

            reply = buffer.CreateMessage();
        }
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        using (var buffer = request.CreateBufferedCopy(int.MaxValue))
        {
            var document = GetDocument(buffer.CreateMessage());
            Logger.LogTrace(document.OuterXml);

            request = buffer.CreateMessage();
            return null;
        }
    }

    private XmlDocument GetDocument(Message request)
    {
        XmlDocument document = new XmlDocument();
        using (MemoryStream memoryStream = new MemoryStream())
        {
            // write request to memory stream
            XmlWriter writer = XmlWriter.Create(memoryStream);
            request.WriteMessage(writer);
            writer.Flush();
            memoryStream.Position = 0;

            // load memory stream into a document
            document.Load(memoryStream);
        }

        return document;
    }
}

Usage:

 if (configuration.GetValue<bool>("Logging:MessageContent"))
     client.Endpoint.EndpointBehaviors.Add(serviceProvider.GetRequiredService<LoggingEndpointBehaviour>());
like image 148
Kaido Avatar answered Sep 22 '22 15:09

Kaido