Skip to content

Instantly share code, notes, and snippets.

@whuanle
Created Jan 9, 2021
Embed
What would you like to do?
using Jaeger;
using Jaeger.Samplers;
using Microsoft.Extensions.Logging;
using OpenTracing;
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
public class Hello
{
private readonly ITracer _tracer;
private readonly ILogger<Hello> _logger;
public Hello(ITracer tracer, ILoggerFactory loggerFactory)
{
_tracer = tracer;
_logger = loggerFactory.CreateLogger<Hello>();
}
public void SayHello(string content)
{
// 创建一个 Span 并开始
var spanBuilder = _tracer.BuildSpan("say-hello");
// ---------------------------------
var span = spanBuilder.Start(); // |
var str = $"Hello,{content}"; // |
_logger.LogInformation(str); // |
span.Finish(); // |
// ---------------------------------
}
}
class Program
{
private static Tracer InitTracer(string serviceName, ILoggerFactory loggerFactory)
{
var samplerConfiguration = new Configuration.SamplerConfiguration(loggerFactory)
.WithType(ConstSampler.Type)
.WithParam(1);
var reporterConfiguration = new Configuration.ReporterConfiguration(loggerFactory)
.WithLogSpans(true);
return (Tracer)new Configuration(serviceName, loggerFactory)
.WithSampler(samplerConfiguration)
.WithReporter(reporterConfiguration)
.GetTracer();
}
static void Main(string[] args)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var tracer = InitTracer("hello-world", loggerFactory);
Hello hello = new Hello(tracer, loggerFactory);
hello.SayHello("This trace");
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment