Giter Club home page Giter Club logo

wcfdistributedtracing's Introduction

WCFDistributedTracing

Publish NuGet

This project shows how distributed tracing in WCF can be used to do structured logging. DistributedOperationContext is used to pass context information through the distributed execution path. Serilog is used as the logging framework, with an output to Seq for central analysis.

This will result in the following output in Seq: Diagram

How to use

Seq

Download and install Seq

Add the WCFDistributedTracing package

Install-Package WCFDistributedTracing

Add the TracingBehavior

var host = new TracingEnabledServiceHost(typeof(SimpleEdgeService), new Uri(SimpleEdgeService.BaseAddress));
var endPoint = host.AddServiceEndpoint(typeof(ISimpleEdgeService), new BasicHttpBinding(), "");
endPoint.AddBehavior<InspectorBehavior<TracingInspector>>();
host.Open();

var factory = new DuplexChannelFactory<ISimplePlatformService>(callbackInstance, new WSDualHttpBinding(), new EndpointAddress(SimplePlatformService.BaseAddress));
factory.Endpoint.AddBehavior<InspectorBehavior<TracingInspector>>();
var proxy = factory.CreateChannel(callbackInstance);

Add the Serilog ContextEnricher

Log.Logger = new LoggerConfiguration()
    .WriteTo.Seq("http://localhost:5341")
    // add the xunit test output sink to the serilog logger
    // https://github.com/trbenning/serilog-sinks-xunit#serilog-sinks-xunit
    .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message:lj} {Properties} {NewLine}{Exception}")
    .Enrich.WithProcessName()
    .Enrich.With<WCFTracingEnricher>()
    .CreateLogger();

Initiate new trace (not required)

When there is not a current DistributedOperationContext during a WCF call a new one will be created with a new TraceId. It is also possible to force a new DistributedOperationContext when it is necessary to run multiple parralel WCF calls with the same TraceId:

var channelFactory = new ChannelFactory<ISimpleEdgeService>(new BasicHttpBinding(), new EndpointAddress(SimpleEdgeService.BaseAddress));
channelFactory.Endpoint.AddBehavior<InspectorBehavior<TracingInspector>>();
var proxy = channelFactory.CreateChannel();

// All WCF operations and logging statements will have the same TraceId after this initialization
DistributedOperationContext.Current = new DistributedOperationContext();

var result = await proxy.Echo($"Hello edge service calling you from operation {traceId}");
Log.Information("Received: {Answer}", result.Message);

Details

Application structure

Diagram

Async pattern

The DistributedOperationContext is scoped using AsyncLocal. That means it flows with async operations downstream, but not upstream if changed downstream.

Duplex channel

Duplex channels are supported. The interaction between the EdgeServer and the PlatformServer shows this.

Binding

The implementation works with different types of bindings, BasicHttpBinding, WSDUalHttpBinding and NetTcpBinding have been tested. The sample services demonstrate the use of a NetTcpBinding for the EdgeService and a WSDualHttpBinding for the PlatormService.

Standards

For the trace context we try to follow the W3C standard as much as possible.

ToDo:

wcfdistributedtracing's People

Contributors

jkosters avatar machielvisser avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

ineba

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.