forked from yurishkuro/opentracing-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloActive.cs
More file actions
70 lines (63 loc) · 2.22 KB
/
Copy pathHelloActive.cs
File metadata and controls
70 lines (63 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using OpenTracing.Tutorial.Library;
namespace OpenTracing.Tutorial.Lesson02.Solution
{
internal class HelloActive
{
private readonly ITracer _tracer;
private readonly ILogger<HelloActive> _logger;
public HelloActive(ITracer tracer, ILoggerFactory loggerFactory)
{
_tracer = tracer;
_logger = loggerFactory.CreateLogger<HelloActive>();
}
private string FormatString(string helloTo)
{
using (var scope = _tracer.BuildSpan("format-string").StartActive(true))
{
var helloString = $"Hello, {helloTo}!";
scope.Span.Log(new Dictionary<string, object>
{
[LogFields.Event] = "string.Format",
["value"] = helloString
});
return helloString;
}
}
private void PrintHello(string helloString)
{
using (var scope = _tracer.BuildSpan("print-hello").StartActive(true))
{
_logger.LogInformation(helloString);
scope.Span.Log("WriteLine");
}
}
public void SayHello(string helloTo)
{
using (var scope = _tracer.BuildSpan("say-hello").StartActive(true))
{
scope.Span.SetTag("hello-to", helloTo);
var helloString = FormatString(helloTo);
PrintHello(helloString);
}
}
// TODO: Rename MainActive to Main to run it. Make sure that HelloManual.cs has MainManual instead of Main, otherwise it will not build!
public static void MainActive(string[] args)
{
if (args.Length != 1)
{
throw new ArgumentException("Expecting one argument");
}
using (var loggerFactory = new LoggerFactory().AddConsole())
{
var helloTo = args[0];
using (var tracer = Tracing.Init("hello-world", loggerFactory))
{
new HelloActive(tracer, loggerFactory).SayHello(helloTo);
}
}
}
}
}