private static void Main(string[] args) { //Reference https://github.com/jaegertracing/jaeger-client-csharp var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); const string serviceName = "initExampleService"; var reporter = new LoggingReporter(loggerFactory); var sampler = new ConstSampler(true); var tracer = new Tracer.Builder(serviceName) .WithLoggerFactory(loggerFactory) .WithReporter(reporter) .WithSampler(sampler) .Build(); const string operationName = "Get::api/values"; var spanBuilder = tracer.BuildSpan(operationName); using (var scope = spanBuilder.StartActive(true)) { var logData = new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("handling number of events", 6), new KeyValuePair <string, object>("using legacy system", false) }; scope.Span.Log(DateTimeOffset.Now, logData); } Console.ReadLine(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddOpenTracing(); services.AddTransient <HttpClient>(); services.AddLogging(configure => { configure.ClearProviders(); var loggerConfiguration = new LoggerConfiguration() .ReadFrom.Configuration(Configuration); //.Enrich.WithExceptionDetails(); //loggerConfiguration = loggerConfiguration // .Enrich.FromLogContext() // .Enrich.WithProcessId() // .Enrich.WithProcessName() // .Enrich.WithThreadId() // .Enrich.WithEnvironmentUserName() // .Enrich.WithMachineName(); loggerConfiguration = loggerConfiguration.WriteTo .Async(a => a.Console(new JsonFormatter())); Log.Logger = loggerConfiguration.CreateLogger(); configure.AddSerilog(); configure.AddDebug(); }); // Adds the Jaeger Tracer. services.AddSingleton <ITracer>(serviceProvider => { var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); string serviceName = serviceProvider.GetRequiredService <IHostingEnvironment>().ApplicationName; var reporter = new LoggingReporter(loggerFactory); var sampler = new ConstSampler(true); // This will log to a default localhost installation of Jaeger. var tracer = new Tracer.Builder(serviceName) //.WithLoggerFactory(loggerFactory) .WithReporter(reporter) .WithSampler(sampler) .Build(); // Allows code that can't use DI to also access the tracer. GlobalTracer.Register(tracer); return(tracer); }); }
public void LoggingReporter_ShouldCallLogger() { var loggerFactory = Substitute.For <ILoggerFactory>(); var logger = Substitute.For <ILogger>(); var span = Substitute.For <IJaegerCoreSpan>(); loggerFactory.CreateLogger <LoggingReporter>().Returns(logger); using (var reporter = new LoggingReporter(loggerFactory)) { loggerFactory.Received(1).CreateLogger <LoggingReporter>(); reporter.Report(span); logger.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); } }
public virtual IReporter GetReporter(IMetrics metrics) { IReporter reporter = new RemoteReporter.Builder() .WithLoggerFactory(_loggerFactory) .WithMetrics(metrics) .WithSender(SenderConfig.GetSender()) .WithFlushInterval(FlushInterval.GetValueOrDefault(RemoteReporter.DefaultFlushInterval)) .WithMaxQueueSize(MaxQueueSize.GetValueOrDefault(RemoteReporter.DefaultMaxQueueSize)) .Build(); if (LogSpans) { IReporter loggingReporter = new LoggingReporter(_loggerFactory); reporter = new CompositeReporter(reporter, loggingReporter); } return(reporter); }
public void LoggingReporter_CanLogActiveSpan() { var loggerFactory = Substitute.For <ILoggerFactory>(); var logger = Substitute.For <ILogger>(); loggerFactory.CreateLogger <LoggingReporter>().Returns(logger); var reporter = new LoggingReporter(loggerFactory); var tracer = new Tracer.Builder("service") .WithReporter(reporter) .WithSampler(new ConstSampler(true)) .Build(); tracer.BuildSpan("foo").StartActive(true).Dispose(); loggerFactory.Received(1).CreateLogger <LoggingReporter>(); logger.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); }
public void LoggingReporter_ShouldCallLogger() { var loggerFactory = Substitute.For <ILoggerFactory>(); var logger = Substitute.For <MockLogger>(); loggerFactory.CreateLogger <LoggingReporter>().Returns <ILogger>(logger); var reporter = new LoggingReporter(loggerFactory); var tracer = new Tracer.Builder("service") .WithReporter(reporter) .WithSampler(new ConstSampler(true)) .Build(); tracer.BuildSpan("foo").Start().Finish(); loggerFactory.Received(1).CreateLogger <LoggingReporter>(); logger.Received(1).Log(LogLevel.Information, Arg.Any <string>()); }
public void CompositeReporter_ShouldCallLoggerOnBoth() { var loggerFactory = Substitute.For <ILoggerFactory>(); var logger1 = Substitute.For <ILogger>(); var logger2 = Substitute.For <ILogger>(); var span = Substitute.For <ILetsTraceSpan>(); loggerFactory.CreateLogger <LoggingReporter>().Returns(logger1, logger2); var reporter1 = new LoggingReporter(loggerFactory); var reporter2 = new LoggingReporter(loggerFactory); using (var reporter = new CompositeReporter(reporter1, reporter2)) { loggerFactory.Received(2).CreateLogger <LoggingReporter>(); reporter.Report(span); logger1.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); logger2.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); } }
public void CompositeReporter_ShouldCallLoggerOnBoth() { var loggerFactory = Substitute.For <ILoggerFactory>(); var logger1 = Substitute.For <ILogger>(); var logger2 = Substitute.For <ILogger>(); loggerFactory.CreateLogger <LoggingReporter>().Returns(logger1, logger2); var reporter1 = new LoggingReporter(loggerFactory); var reporter2 = new LoggingReporter(loggerFactory); var reporter = new CompositeReporter(reporter1, reporter2); var tracer = new Tracer.Builder("service") .WithReporter(reporter) .WithSampler(new ConstSampler(true)) .Build(); tracer.BuildSpan("foo").Start().Finish(); loggerFactory.Received(2).CreateLogger <LoggingReporter>(); logger1.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); logger2.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >()); }
public void TestConsumer() { var cf = new ConnectionFactory() { Uri = new Uri("amqp://*****:*****@localhost:5672") }; var serviceName = "initExampleService"; var transport = new JaegerHttpTransport("192.168.99.100"); //var zt = new ZipkinJSONTransport(new Uri("http://192.168.99.100:9411"), 2); var reporter = new RemoteReporter.Builder(transport).Build(); //var zreporter = new RemoteReporter.Builder(zt).Build(); var sampler = new ConstSampler(true); var serviceProvider = new ServiceCollection() .AddLogging() //<-- You were missing this .BuildServiceProvider(); //get logger var loggerFactory = serviceProvider.GetService <ILoggerFactory>(); loggerFactory.AddConsole(LogLevel.Trace); loggerFactory.AddDebug(LogLevel.Trace); var log = loggerFactory.CreateLogger("test"); log.LogInformation("prova log {0}", "prova"); var logReporter = new LoggingReporter(loggerFactory); var tracer = new Tracer.Builder(serviceName) .WithLoggerFactory(loggerFactory) //.WithTransport(transport) .WithReporter(logReporter) .WithSampler(sampler) .Build(); var conn = cf.CreateConnection("testtracing"); var model = new TracingModel(conn.CreateModel(), tracer); // TODO: Add your test code here model.BasicPublish("testexchange", "", false, null, Encoding.UTF8.GetBytes("testmessage")); var consumer = new EventingBasicConsumer(model); consumer.Received += (ch, ea) => { var body = ea.Body; // ... process the message model.BasicAck(ea.DeliveryTag, false); }; var trConsumer = new TracingConsumer(consumer, tracer); Task.Delay(200).Wait(); var res = model.BasicConsume("testqueueu", false, trConsumer); Assert.True(true, "Your first passing test"); }