Skip to content

Commit 0716cc8

Browse files
authored
Updating logging-notification sample to NET 10 (#7462)
1 parent 581acaf commit 0716cc8

File tree

8 files changed

+164
-1
lines changed

8 files changed

+164
-1
lines changed
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}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(SolutionProperties) = preSolution
17+
HideSolutionNode = FALSE
18+
EndGlobalSection
19+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using NServiceBus;
5+
6+
public class MyHandler(ILogger<MyHandler> logger) :
7+
IHandleMessages<MyMessage>
8+
{
9+
public Task Handle(MyMessage message, IMessageHandlerContext context)
10+
{
11+
logger.LogCritical("Received message with property: {Property}", message.Property);
12+
throw new Exception("The exception message");
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using NServiceBus;
2+
3+
public class MyMessage :
4+
IMessage
5+
{
6+
public string Property { get; set; }
7+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using NServiceBus;
6+
7+
8+
var host = Host.CreateDefaultBuilder(args)
9+
.ConfigureServices((hostContext, services) =>
10+
{
11+
#region logging
12+
services.AddLogging(x =>
13+
{
14+
x.SetMinimumLevel(LogLevel.Critical);
15+
});
16+
#endregion
17+
18+
})
19+
.UseConsoleLifetime()
20+
.UseNServiceBus(context =>
21+
{
22+
#region endpointConfig
23+
24+
var endpointConfiguration = new EndpointConfiguration("Samples.Notifications");
25+
SubscribeToNotifications.Subscribe(endpointConfiguration);
26+
#endregion
27+
28+
endpointConfiguration.UsePersistence<LearningPersistence>();
29+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
30+
endpointConfiguration.UseTransport(new LearningTransport());
31+
32+
#region customDelayedRetries
33+
34+
var recoverability = endpointConfiguration.Recoverability();
35+
recoverability.Delayed(
36+
customizations: delayed =>
37+
{
38+
delayed.TimeIncrease(TimeSpan.FromSeconds(1));
39+
});
40+
41+
#endregion
42+
43+
return endpointConfiguration;
44+
}).Build();
45+
46+
47+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
48+
49+
var message = new MyMessage
50+
{
51+
Property = "PropertyValue"
52+
};
53+
54+
await messageSession.SendLocal(message);
55+
56+
Console.WriteLine("Press Ctrl+C to exit");
57+
await host.RunAsync();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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.Extensions.Hosting" Version="4.0.0-alpha.1" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using NServiceBus;
6+
using NServiceBus.Faults;
7+
using NServiceBus.Logging;
8+
9+
#region subscriptions
10+
11+
public static class SubscribeToNotifications
12+
{
13+
static ILog log = LogManager.GetLogger(typeof(SubscribeToNotifications));
14+
15+
public static void Subscribe(EndpointConfiguration endpointConfiguration)
16+
{
17+
var recoverability = endpointConfiguration.Recoverability();
18+
recoverability.Immediate(settings => settings.OnMessageBeingRetried(Log));
19+
recoverability.Delayed(settings => settings.OnMessageBeingRetried(Log));
20+
recoverability.Failed(settings => settings.OnMessageSentToErrorQueue(Log));
21+
}
22+
23+
static string GetMessageString(ReadOnlyMemory<byte> body)
24+
{
25+
return Encoding.UTF8.GetString(body.ToArray());
26+
}
27+
28+
static Task Log(FailedMessage failed, CancellationToken cancellationToken)
29+
{
30+
log.Fatal($@"Message sent to error queue.
31+
Body:
32+
{GetMessageString(failed.Body)}");
33+
return Task.CompletedTask;
34+
}
35+
36+
static Task Log(DelayedRetryMessage retry, CancellationToken cancellationToken)
37+
{
38+
log.Fatal($@"Message sent to Delayed Retries.
39+
RetryAttempt:{retry.RetryAttempt}
40+
Body:
41+
{GetMessageString(retry.Body)}");
42+
return Task.CompletedTask;
43+
}
44+
45+
static Task Log(ImmediateRetryMessage retry, CancellationToken cancellationToken)
46+
{
47+
log.Fatal($@"Message sent to Immediate Retry.
48+
RetryAttempt:{retry.RetryAttempt}
49+
Body:
50+
{GetMessageString(retry.Body)}");
51+
return Task.CompletedTask;
52+
}
53+
}
54+
55+
#endregion

samples/logging/notifications/Core_10/prerelease.txt

Whitespace-only changes.

samples/logging/notifications/sample.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This sample uses several non-standard settings.
2020

2121
### Logging
2222

23-
All errors below Fatal are suppressed to reduce the noise related to raising multiple exceptions
23+
All errors below Critical are suppressed to reduce the noise related to raising multiple exceptions
2424

2525
snippet: logging
2626

0 commit comments

Comments
 (0)