Skip to content

Commit d99f974

Browse files
authored
Merge pull request #7586 from Particular/nsb10-logging-metrics
Add Metrics Logging sample for NServiceBus 10
2 parents 1aa0e8c + 1169729 commit d99f974

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
9+
<PackageReference Include="NServiceBus.Metrics" Version="6.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Diagnostics;
3+
using NServiceBus;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
Console.Title = "TracingEndpoint";
8+
9+
var host = Host.CreateDefaultBuilder(args)
10+
.UseConsoleLifetime()
11+
.UseNServiceBus(_ =>
12+
{
13+
var endpointConfiguration = new EndpointConfiguration("Samples.Metrics.Tracing.Endpoint");
14+
15+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
16+
endpointConfiguration.UseTransport<LearningTransport>();
17+
18+
#region EnableMetricTracing
19+
20+
var metrics = endpointConfiguration.EnableMetrics();
21+
metrics.RegisterObservers(
22+
register: context =>
23+
{
24+
foreach (var duration in context.Durations)
25+
{
26+
duration.Register(
27+
observer: (ref DurationEvent @event) =>
28+
{
29+
Trace.WriteLine(
30+
$"Duration: '{duration.Name}'. Value: '{@event.Duration}' ({@event.MessageType})");
31+
});
32+
}
33+
34+
foreach (var signal in context.Signals)
35+
{
36+
signal.Register(
37+
observer: (ref SignalEvent @event) =>
38+
{
39+
Trace.WriteLine($"Signal: '{signal.Name}' ({@event.MessageType})");
40+
});
41+
}
42+
});
43+
44+
#endregion
45+
46+
return endpointConfiguration;
47+
})
48+
.Build();
49+
50+
await host.StartAsync();
51+
52+
var endpointInstance = host.Services.GetRequiredService<IMessageSession>();
53+
54+
try
55+
{
56+
Console.WriteLine("Endpoint started. Press 'enter' to send a message");
57+
Console.WriteLine("Press any other key to quit");
58+
59+
while (true)
60+
{
61+
var key = Console.ReadKey(true);
62+
if (key.Key != ConsoleKey.Enter)
63+
{
64+
break;
65+
}
66+
67+
await endpointInstance.SendLocal(new SomeCommand());
68+
}
69+
}
70+
finally
71+
{
72+
await host.StopAsync();
73+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using NServiceBus;
2+
3+
class SomeCommand : ICommand;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using NServiceBus.Logging;
4+
5+
class SomeCommandHandler :
6+
IHandleMessages<SomeCommand>
7+
{
8+
static ILog log = LogManager.GetLogger<SomeCommandHandler>();
9+
10+
public Task Handle(SomeCommand message, IMessageHandlerContext context)
11+
{
12+
log.Info("Hello from SomeCommandHandler");
13+
return Task.CompletedTask;
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29728.190
5+
MinimumVisualStudioVersion = 15.0.26730.12
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Endpoint", "Endpoint\Endpoint.csproj", "{FF5BEF96-88BA-470D-975A-4EF710EE2B7A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{FF5BEF96-88BA-470D-975A-4EF710EE2B7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{FF5BEF96-88BA-470D-975A-4EF710EE2B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(SolutionProperties) = preSolution
17+
HideSolutionNode = FALSE
18+
EndGlobalSection
19+
EndGlobal

samples/logging/metrics/Metrics_6/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)