Skip to content

Commit 6e28b48

Browse files
committed
Extensions Logging sample for NServiceBus 10 #7322
1 parent a95c410 commit 6e28b48

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.1.32210.238
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ExtensibilityGlobals) = postSolution
19+
SolutionGuid = {3673C976-8AFC-4169-A103-E016521D0868}
20+
EndGlobalSection
21+
EndGlobal
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+
public class MyHandler :
6+
IHandleMessages<MyMessage>
7+
{
8+
static ILog log = LogManager.GetLogger<MyHandler>();
9+
10+
public Task Handle(MyMessage message, IMessageHandlerContext context)
11+
{
12+
log.Warn("Hello from MyHandler");
13+
return Task.CompletedTask;
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using NServiceBus;
2+
3+
public class MyMessage :
4+
IMessage
5+
{
6+
}
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 NLog.Config;
5+
using NLog.Extensions.Logging;
6+
using NLog.Targets;
7+
using NServiceBus;
8+
using NServiceBus.Extensions.Logging;
9+
10+
Console.Title = "ExtensionsLogging";
11+
12+
var host = Host.CreateDefaultBuilder(args)
13+
.UseConsoleLifetime()
14+
.UseNServiceBus(_ =>
15+
{
16+
#region NLogConfiguration
17+
var config = new LoggingConfiguration();
18+
19+
var consoleTarget = new ColoredConsoleTarget
20+
{
21+
Layout = "${level}|${logger}|${message}${onexception:${newline}${exception:format=tostring}}"
22+
};
23+
config.AddTarget("console", consoleTarget);
24+
config.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Debug, consoleTarget));
25+
26+
NLog.LogManager.Configuration = config;
27+
#endregion
28+
29+
#region MicrosoftExtensionsLoggingNLog
30+
Microsoft.Extensions.Logging.ILoggerFactory extensionsLoggerFactory = new NLogLoggerFactory();
31+
32+
NServiceBus.Logging.ILoggerFactory nservicebusLoggerFactory =
33+
new ExtensionsLoggerFactory(loggerFactory: extensionsLoggerFactory);
34+
35+
NServiceBus.Logging.LogManager.UseFactory(loggerFactory: nservicebusLoggerFactory);
36+
#endregion
37+
38+
var endpointConfiguration = new EndpointConfiguration("Samples.Logging.ExtensionsLogging");
39+
40+
endpointConfiguration.UseTransport<LearningTransport>();
41+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
42+
43+
return endpointConfiguration;
44+
})
45+
.Build();
46+
47+
await host.StartAsync();
48+
49+
var endpointInstance = host.Services.GetRequiredService<IMessageSession>();
50+
51+
var myMessage = new MyMessage();
52+
await endpointInstance.SendLocal(myMessage);
53+
54+
Console.WriteLine("Press any key to exit");
55+
Console.ReadKey();
56+
57+
await host.StopAsync();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
<PackageReference Include="NServiceBus.Extensions.Logging" Version="4.0.0-alpha.1" />
11+
<PackageReference Include="NLog.Extensions.Logging" Version="6.0.2" />
12+
</ItemGroup>
13+
</Project>

samples/logging/extensions-logging/Extensions.Logging_4/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)