public static class Program
{
public const string SERVICE_NAME = "MyService";
public const string SERVICE_VERSION = "1.0.0";
public static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(new ActivitySource(SERVICE_NAME));
serviceCollection.AddSingleton<TestLogic>();
var tracerProvider = Sdk.CreateTracerProviderBuilder().AddSource(SERVICE_NAME)
.ConfigureResource(resource => resource.AddService(serviceName: SERVICE_NAME, serviceVersion: SERVICE_VERSION))
.AddConsoleExporter()
.Build();
serviceCollection.AddSingleton(tracerProvider);
var services = serviceCollection.BuildServiceProvider();
var helloWorld = await services.GetRequiredService<TestLogic>().HelloWorldMethod();
Console.WriteLine(helloWorld);
Console.WriteLine("Program ended");
}
}
public class TestLogic(ActivitySource activitySource)
{
private readonly ActivitySource _activitySource = activitySource;
public async Task<string> HelloWorldMethod()
{
using var activity = _activitySource.StartActivity("HelloWorldMethod");
await Task.Delay(1000);
return "Hello World";
}
}
public static class Program
{
public const string SERVICE_NAME = "MyService";
public const string SERVICE_VERSION = "1.0.0";
public static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(new ActivitySource(SERVICE_NAME));
serviceCollection.AddSingleton<TestLogic>();
var tracerProvider = Sdk.CreateTracerProviderBuilder().AddSource(SERVICE_NAME)
.ConfigureResource(resource => resource.AddService(serviceName: SERVICE_NAME, serviceVersion: SERVICE_VERSION))
.AddConsoleExporter()
.Build();
serviceCollection.AddSingleton(tracerProvider);
var services = serviceCollection.BuildServiceProvider();
var helloWorld = await services.GetRequiredService<TestLogic>().HelloWorldMethod();
Console.WriteLine(helloWorld);
Console.WriteLine("Program ended");
}
}
public class TestLogic(ActivitySource activitySource)
{
private readonly ActivitySource _activitySource = activitySource;
public async Task<string> HelloWorldMethod()
{
using var activity = _activitySource.StartActivity("HelloWorldMethod");
await Task.Delay(1000);
return "Hello World";
}
}