forked from yurishkuro/opentracing-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloManual.cs
More file actions
79 lines (72 loc) · 2.41 KB
/
HelloManual.cs
File metadata and controls
79 lines (72 loc) · 2.41 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
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using OpenTracing.Tutorial.Library;
namespace OpenTracing.Tutorial.Lesson02.Solution
{
internal class HelloManual
{
private readonly ITracer _tracer;
private readonly ILogger<HelloManual> _logger;
public HelloManual(ITracer tracer, ILoggerFactory loggerFactory)
{
_tracer = tracer;
_logger = loggerFactory.CreateLogger<HelloManual>();
}
private string FormatString(ISpan rootSpan, string helloTo)
{
var span = _tracer.BuildSpan("format-string").AsChildOf(rootSpan).Start();
try
{
var helloString = $"Hello, {helloTo}!";
span.Log(new Dictionary<string, object>
{
[LogFields.Event] = "string.Format",
["value"] = helloString
});
return helloString;
}
finally
{
span.Finish();
}
}
private void PrintHello(ISpan rootSpan, string helloString)
{
var span = _tracer.BuildSpan("print-hello").AsChildOf(rootSpan).Start();
try
{
_logger.LogInformation(helloString);
span.Log("WriteLine");
}
finally
{
span.Finish();
}
}
public void SayHello(string helloTo)
{
var span = _tracer.BuildSpan("say-hello").Start();
span.SetTag("hello-to", helloTo);
var helloString = FormatString(span, helloTo);
PrintHello(span, helloString);
span.Finish();
}
// TODO: Rename MainManual to Main to run it. Make sure that HelloActive.cs has MainActive instead of Main, otherwise it will not build!
public static void MainManual(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 HelloManual(tracer, loggerFactory).SayHello(helloTo);
}
}
}
}
}