diff --git a/nservicebus/best-practices.md b/nservicebus/best-practices.md index 9b9eaaa6b95..52b8442b81c 100644 --- a/nservicebus/best-practices.md +++ b/nservicebus/best-practices.md @@ -1,7 +1,7 @@ --- title: Best practices summary: An assortment of best practices presented as DO, DO NOT, and CONSIDER. -reviewed: 2024-01-05 +reviewed: 2025-10-03 isLearningPath: true --- @@ -13,9 +13,9 @@ This article presents recommendations to keep in mind when designing a system us Multiple message handlers can be combined inside a single logical endpoint. However, these handlers all share a single message queue for different types of messages. -The endpoint is the fundamental unit of scale for an NServiceBus system. If one message handler has much higher throughput requirements, it can only be independently scaled if it exists in an endpoint by itself. In separate endpoints, only the message handler that has the unique scalability requirements has to be scaled out. +The endpoint is the fundamental unit of scale for an NServiceBus system. If one message handler has much higher throughput requirements, it can only be independently scaled if it exists in an endpoint by itself. With separate endpoints, only the message handler that has the unique scalability requirements has to be scaled out. -The endpoint is also the fundamental unit of deployment for an NServcieBus system. That means that the entire endpoint must be redeployed if a fix is required for one message handler. The fewer message handlers in each endpoint, the less likely any individual deployment is to cause a problem in the system since the whole system does not have to be redeployed on every change. +The endpoint is also the fundamental unit of deployment for an NServiceBus system. That means that the entire endpoint must be redeployed if a fix is required for one message handler. The fewer message handlers in each endpoint, the less likely any individual deployment is to cause a problem in the system since the whole system does not have to be redeployed on every change. ### :heavy_check_mark: **CONSIDER grouping message handlers by SLA** @@ -43,11 +43,11 @@ Meanwhile, a custom abstraction makes the NServiceBus documentation less effecti It is best to embrace the asynchronous nature of NServiceBus messages and not use an asynchronous message (or a pair of messages in a request/reply scenario) for synchronous communication, especially when the scenario expects an answer to be available _right now_. This is especially important with queries: [messaging should not be used for queries](https://web.archive.org/web/20211205190919/http://andreasohlund.net/2010/04/22/messaging-shouldnt-be-used-for-queries/). -When a previously-defined user interface demands an immediate response, such as inserting a new item into a grid and then immediately refreshing the grid to include the new item, the [client-side callbacks package](/nservicebus/messaging/callbacks.md) can be used, but this should be considered a crutch until a more [task- or command-focused UI](https://cqrs.wordpress.com/documents/task-based-ui/) can replace it. +When a previously defined user interface demands an immediate response, such as inserting a new item into a grid and then immediately refreshing the grid to include the new item, the [client-side callbacks package](/nservicebus/messaging/callbacks.md) can be used, but this should be considered a temporary workaround until a more [task- or command-focused UI](https://cqrs.wordpress.com/documents/task-based-ui/) can replace it. ### :x: **DO NOT create a messaging endpoint for every single web request** -NServiceBus does a lot of work when it first starts up, scanning through assemblies to find the types of messages and message handlers, establishing communication with the messaging infrastructure, and ensuring that everything is optimized to run quickly for the duration of the endpoint's life. Do not repeat all this work on every web request just to send a single message and then shut down. +NServiceBus does a lot of work when it first starts up, including scanning through assemblies to find the types of messages and message handlers, establishing communication with the messaging infrastructure, and ensuring that everything is optimized to run quickly for the duration of the endpoint's life. Do not repeat all this work on every web request just to send a single message and then shut down. An NServiceBus endpoint is designed to be a long-lived object that persists throughout the application process. Once the `IMessageSession` is created, use dependency injection to inject it into controllers or wherever else it is needed. If necessary, assign the `IMessageSession` to a global variable. @@ -63,7 +63,7 @@ Asynchronous messaging (e.g., NServiceBus) is **not** a good solution for data d Message handlers should be simple and focused on only the business code needed to handle the message. Infrastructure code for logging, exception management, timing, auditing, authorization, unit of work, message signing/encryption, etc, should not be included in a message handler. -Instead, implement this functionality separately in a [message pipeline behavior](/nservicebus/pipeline/manipulate-with-behaviors.md), which enables inserting additional functionality into the NServiceBus message processing pipeline, similar to an ASP.NET ActionFilter. +Instead, implement this functionality separately in a [message pipeline behavior](/nservicebus/pipeline/manipulate-with-behaviors.md), which enables inserting additional functionality into the NServiceBus message processing pipeline. For a high-level overview of infrastructure concerns and behaviors, see the blog post [Infrastructure soup](https://particular.net/blog/infrastructure-soup). @@ -98,21 +98,17 @@ Message queues are long-lasting and durable. Occasionally connected clients, suc For occasionally connected clients, consider another communication medium, such as in the [Near real-time transient clients sample](/samples/near-realtime-clients/), which communicates with clients using [SignalR](https://dotnet.microsoft.com/apps/aspnet/signalr). -### :heavy_check_mark: **CONSIDER [identifying message types using conventions](/nservicebus/messaging/unobtrusive-mode.md) to make upgrading to new versions of NServiceBus easier** +### :heavy_check_mark: **Consider using shared message assemblies that reference [NServiceBus.MessageInterfaces](https://www.nuget.org/packages/NServiceBus.MessageInterfaces) to make upgrading to new versions of NServiceBus easier** -By default, NServiceBus will identify classes implementing `ICommand` as commands, `IEvent` as events, and `IMessage` as other types of messages, such as replies. This is quick and easy but also causes message projects to depend on the NServiceBus NuGet package. +In a complex system, it's useful to be able to upgrade one endpoint at a time. Message assemblies are shared between multiple endpoints. If these assemblies reference the NServiceBus NuGet package directly, they can cause challenges during upgrades when one endpoint using a message assembly has upgraded to the next major version but the other has not. -In a complex system, it's useful to be able to upgrade one endpoint at a time. Message assemblies are shared between multiple endpoints, which can cause challenges during upgrades when one endpoint using a message assembly has upgraded to the next major version but the other has not. - -These versioning problems can be addressed using [unobtrusive-mode messages](/nservicebus/messaging/unobtrusive-mode.md) by defining [message conventions](/nservicebus/messaging/conventions.md) independent of the `ICommand`/`IEvent`/`IMessage` interfaces. - -These conventions can even be [encapsulated in a class](/nservicebus/messaging/conventions.md#encapsulated-conventions), and many can be used within one endpoint so that messages from multiple teams who have made different choices on message conventions can be used together. +To solve this, message assemblies should instead reference the [`NServiceBus.MessageInterfaces` package](/samples/message-assembly-sharing/) which has no dependency on the NServiceBus package. ## System monitoring -### :heavy_check_mark: **DO install the [Heartbeats plugin](/monitoring/heartbeats/) in all endpoints to monitor for endpoint health** +### :heavy_check_mark: **DO install the [Heartbeat plugin](/monitoring/heartbeats/) in all endpoints to monitor for endpoint health** -The Heartbeats plugin sends a message to [ServiceControl](/servicecontrol/) at regular intervals to demonstrate that the process is not only executing (which would be provided by any suite of system monitoring tools) but is also capable of interacting with the message transport. +The Heartbeat plugin sends a message to [ServiceControl](/servicecontrol/) at regular intervals to demonstrate that the process is not only executing (which would be provided by any suite of system monitoring tools) but is also capable of interacting with the message transport. If ServiceControl stops receiving heartbeat messages from an endpoint, that endpoint will be shown as [inactive in ServicePulse](/monitoring/heartbeats/in-servicepulse.md). In addition, ServiceControl will publish a [`HeartbeatStopped` event](/monitoring/heartbeats/notification-events.md) so that operations staff can be notified and respond. diff --git a/nservicebus/compliance/business-continuity.md b/nservicebus/compliance/business-continuity.md index c7862bcc6bf..977df56a48f 100644 --- a/nservicebus/compliance/business-continuity.md +++ b/nservicebus/compliance/business-continuity.md @@ -1,7 +1,7 @@ --- title: Business continuity and disaster recovery procedures summary: Particular Software business continuity and disaster recovery procedures -reviewed: 2024-01-10 +reviewed: 2025-10-03 --- This document describes Particular Software business continuity and disaster recovery procedures. diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Greeter.cs b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Greeter.cs index 7d26eed7bca..105f9492b9e 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Greeter.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Greeter.cs @@ -2,7 +2,7 @@ public class Greeter { - private static readonly ILog log = LogManager.GetLogger(); + static readonly ILog log = LogManager.GetLogger(); public void SayHello() { diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MessageSender.cs b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MessageSender.cs index 07e37142924..4c0d33ca78a 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MessageSender.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MessageSender.cs @@ -1,7 +1,4 @@ -using System.Threading.Tasks; -using NServiceBus; - -#region InjectingMessageSession +#region InjectingMessageSession public class MessageSender(IMessageSession messageSession) { public Task SendMessage() diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyHandler.cs b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyHandler.cs index 0a0f0a37382..a506df30dc1 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyHandler.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyHandler.cs @@ -1,8 +1,5 @@ -using System.Threading.Tasks; -using NServiceBus; -#region InjectingDependency -public class MyHandler(Greeter greeter) : - IHandleMessages +#region InjectingDependency +public class MyHandler(Greeter greeter) : IHandleMessages { public Task Handle(MyMessage message, IMessageHandlerContext context) { diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyMessage.cs b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyMessage.cs index 7ee00d20323..50cb56a5b56 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyMessage.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/MyMessage.cs @@ -1,3 +1 @@ -using NServiceBus; - -public record MyMessage : IMessage; \ No newline at end of file +public record MyMessage : IMessage; \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Program.cs b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Program.cs index b03e0c8723a..da53032fb15 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Program.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Program.cs @@ -1,47 +1,38 @@ -using System; -using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using NServiceBus; -static class Program -{ - static async Task Main() - { - Console.Title = "ExternallyManagedContainer"; +Console.Title = "ExternallyManagedContainer"; - var endpointConfiguration = new EndpointConfiguration("Sample"); - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(new LearningTransport()); +var endpointConfiguration = new EndpointConfiguration("Sample"); +endpointConfiguration.UseSerialization(); +endpointConfiguration.UseTransport(new LearningTransport()); - #region ContainerConfiguration +#region ContainerConfiguration - // ServiceCollection is provided by Microsoft.Extensions.DependencyInjection - var serviceCollection = new ServiceCollection(); +// ServiceCollection is provided by Microsoft.Extensions.DependencyInjection +var serviceCollection = new ServiceCollection(); - // most dependencies may now be registered - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); +// most dependencies may now be registered +serviceCollection.AddSingleton(); +serviceCollection.AddSingleton(); - // EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, - // which is inherited by ServiceCollection - var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer - .Create(endpointConfiguration, serviceCollection); +// EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, +// which is inherited by ServiceCollection +var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer + .Create(endpointConfiguration, serviceCollection); - // if IMessageSession is required as dependency, it may now be registered - serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); +// if IMessageSession is required as dependency, it may now be registered +serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); - #endregion +#endregion - using (var serviceProvider = serviceCollection.BuildServiceProvider()) - { - var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); +using var serviceProvider = serviceCollection.BuildServiceProvider(); - var sender = serviceProvider.GetRequiredService(); - await sender.SendMessage(); +var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - await endpoint.Stop(); - } - } -} +var sender = serviceProvider.GetRequiredService(); +await sender.SendMessage(); + +Console.WriteLine("Press any key to exit"); +Console.ReadKey(); + +await endpoint.Stop(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Sample.csproj b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Sample.csproj index dd9d35b6dda..28d2b7dd297 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Sample.csproj +++ b/samples/dependency-injection/externally-managed-mode/Core_10/Sample/Sample.csproj @@ -1,10 +1,14 @@ + net10.0 Exe + enable 14.0 + + \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Greeter.cs b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Greeter.cs index 7d26eed7bca..105f9492b9e 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Greeter.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Greeter.cs @@ -2,7 +2,7 @@ public class Greeter { - private static readonly ILog log = LogManager.GetLogger(); + static readonly ILog log = LogManager.GetLogger(); public void SayHello() { diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MessageSender.cs b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MessageSender.cs index b6b3d09d72a..4c0d33ca78a 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MessageSender.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MessageSender.cs @@ -1,14 +1,6 @@ -using System.Threading.Tasks; -using NServiceBus; - -#region InjectingMessageSession -public class MessageSender +#region InjectingMessageSession +public class MessageSender(IMessageSession messageSession) { - private readonly IMessageSession messageSession; - - public MessageSender(IMessageSession messageSession) => - this.messageSession = messageSession; - public Task SendMessage() { var myMessage = new MyMessage(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyHandler.cs b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyHandler.cs index 403be766a98..a506df30dc1 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyHandler.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyHandler.cs @@ -1,14 +1,6 @@ -using System.Threading.Tasks; -using NServiceBus; -#region InjectingDependency -public class MyHandler : - IHandleMessages +#region InjectingDependency +public class MyHandler(Greeter greeter) : IHandleMessages { - private readonly Greeter greeter; - - public MyHandler(Greeter greeter) => - this.greeter = greeter; - public Task Handle(MyMessage message, IMessageHandlerContext context) { greeter.SayHello(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyMessage.cs b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyMessage.cs index 1686f5539f7..d80f5ce2509 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyMessage.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/MyMessage.cs @@ -1,5 +1 @@ -using NServiceBus; - -public class MyMessage : IMessage -{ -} \ No newline at end of file +public class MyMessage : IMessage; \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Program.cs b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Program.cs index b03e0c8723a..da53032fb15 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Program.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Program.cs @@ -1,47 +1,38 @@ -using System; -using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using NServiceBus; -static class Program -{ - static async Task Main() - { - Console.Title = "ExternallyManagedContainer"; +Console.Title = "ExternallyManagedContainer"; - var endpointConfiguration = new EndpointConfiguration("Sample"); - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(new LearningTransport()); +var endpointConfiguration = new EndpointConfiguration("Sample"); +endpointConfiguration.UseSerialization(); +endpointConfiguration.UseTransport(new LearningTransport()); - #region ContainerConfiguration +#region ContainerConfiguration - // ServiceCollection is provided by Microsoft.Extensions.DependencyInjection - var serviceCollection = new ServiceCollection(); +// ServiceCollection is provided by Microsoft.Extensions.DependencyInjection +var serviceCollection = new ServiceCollection(); - // most dependencies may now be registered - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); +// most dependencies may now be registered +serviceCollection.AddSingleton(); +serviceCollection.AddSingleton(); - // EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, - // which is inherited by ServiceCollection - var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer - .Create(endpointConfiguration, serviceCollection); +// EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, +// which is inherited by ServiceCollection +var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer + .Create(endpointConfiguration, serviceCollection); - // if IMessageSession is required as dependency, it may now be registered - serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); +// if IMessageSession is required as dependency, it may now be registered +serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); - #endregion +#endregion - using (var serviceProvider = serviceCollection.BuildServiceProvider()) - { - var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); +using var serviceProvider = serviceCollection.BuildServiceProvider(); - var sender = serviceProvider.GetRequiredService(); - await sender.SendMessage(); +var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - await endpoint.Stop(); - } - } -} +var sender = serviceProvider.GetRequiredService(); +await sender.SendMessage(); + +Console.WriteLine("Press any key to exit"); +Console.ReadKey(); + +await endpoint.Stop(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Sample.csproj b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Sample.csproj index cd288b0a577..0f8fd657baa 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Sample.csproj +++ b/samples/dependency-injection/externally-managed-mode/Core_8/Sample/Sample.csproj @@ -1,10 +1,14 @@ + net9.0;net8.0;net48 Exe + enable 12.0 + + \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Greeter.cs b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Greeter.cs index 7d26eed7bca..105f9492b9e 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Greeter.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Greeter.cs @@ -2,7 +2,7 @@ public class Greeter { - private static readonly ILog log = LogManager.GetLogger(); + static readonly ILog log = LogManager.GetLogger(); public void SayHello() { diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MessageSender.cs b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MessageSender.cs index b6b3d09d72a..4c0d33ca78a 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MessageSender.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MessageSender.cs @@ -1,14 +1,6 @@ -using System.Threading.Tasks; -using NServiceBus; - -#region InjectingMessageSession -public class MessageSender +#region InjectingMessageSession +public class MessageSender(IMessageSession messageSession) { - private readonly IMessageSession messageSession; - - public MessageSender(IMessageSession messageSession) => - this.messageSession = messageSession; - public Task SendMessage() { var myMessage = new MyMessage(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyHandler.cs b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyHandler.cs index 403be766a98..a506df30dc1 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyHandler.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyHandler.cs @@ -1,14 +1,6 @@ -using System.Threading.Tasks; -using NServiceBus; -#region InjectingDependency -public class MyHandler : - IHandleMessages +#region InjectingDependency +public class MyHandler(Greeter greeter) : IHandleMessages { - private readonly Greeter greeter; - - public MyHandler(Greeter greeter) => - this.greeter = greeter; - public Task Handle(MyMessage message, IMessageHandlerContext context) { greeter.SayHello(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyMessage.cs b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyMessage.cs index 1686f5539f7..d80f5ce2509 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyMessage.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/MyMessage.cs @@ -1,5 +1 @@ -using NServiceBus; - -public class MyMessage : IMessage -{ -} \ No newline at end of file +public class MyMessage : IMessage; \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Program.cs b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Program.cs index b03e0c8723a..da53032fb15 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Program.cs +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Program.cs @@ -1,47 +1,38 @@ -using System; -using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using NServiceBus; -static class Program -{ - static async Task Main() - { - Console.Title = "ExternallyManagedContainer"; +Console.Title = "ExternallyManagedContainer"; - var endpointConfiguration = new EndpointConfiguration("Sample"); - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(new LearningTransport()); +var endpointConfiguration = new EndpointConfiguration("Sample"); +endpointConfiguration.UseSerialization(); +endpointConfiguration.UseTransport(new LearningTransport()); - #region ContainerConfiguration +#region ContainerConfiguration - // ServiceCollection is provided by Microsoft.Extensions.DependencyInjection - var serviceCollection = new ServiceCollection(); +// ServiceCollection is provided by Microsoft.Extensions.DependencyInjection +var serviceCollection = new ServiceCollection(); - // most dependencies may now be registered - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); +// most dependencies may now be registered +serviceCollection.AddSingleton(); +serviceCollection.AddSingleton(); - // EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, - // which is inherited by ServiceCollection - var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer - .Create(endpointConfiguration, serviceCollection); +// EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection, +// which is inherited by ServiceCollection +var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer + .Create(endpointConfiguration, serviceCollection); - // if IMessageSession is required as dependency, it may now be registered - serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); +// if IMessageSession is required as dependency, it may now be registered +serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value); - #endregion +#endregion - using (var serviceProvider = serviceCollection.BuildServiceProvider()) - { - var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); +using var serviceProvider = serviceCollection.BuildServiceProvider(); - var sender = serviceProvider.GetRequiredService(); - await sender.SendMessage(); +var endpoint = await endpointWithExternallyManagedContainer.Start(serviceProvider); - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - await endpoint.Stop(); - } - } -} +var sender = serviceProvider.GetRequiredService(); +await sender.SendMessage(); + +Console.WriteLine("Press any key to exit"); +Console.ReadKey(); + +await endpoint.Stop(); diff --git a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Sample.csproj b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Sample.csproj index 839a535ac53..fe0ff11a32a 100644 --- a/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Sample.csproj +++ b/samples/dependency-injection/externally-managed-mode/Core_9/Sample/Sample.csproj @@ -1,10 +1,14 @@ + net9.0;net8.0 Exe + enable 12.0 + + \ No newline at end of file diff --git a/samples/dependency-injection/externally-managed-mode/sample.md b/samples/dependency-injection/externally-managed-mode/sample.md index eb85c128c53..536c8a7982a 100644 --- a/samples/dependency-injection/externally-managed-mode/sample.md +++ b/samples/dependency-injection/externally-managed-mode/sample.md @@ -1,15 +1,15 @@ --- title: Externally Managed Mode -summary: A sample that uses NServiceBus's externally managed mode feature to configure a DI container. +summary: A sample that uses the externally managed mode feature to configure a dependency injection container. component: Core -reviewed: 2023-12-18 +reviewed: 2025-10-03 related: - nservicebus/dependency-injection --- ### Configuring the endpoint -The sample configures an endpoint to use [externally managed mode](/nservicebus/dependency-injection/#modes-of-operation-externally-managed-mode) with [Microsoft's dependency injection container](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection) and registers some dependencies. +This sample configures an endpoint to use [externally managed mode](/nservicebus/dependency-injection/#modes-of-operation-externally-managed-mode) with [Microsoft's dependency injection container](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection) and registers some dependencies. snippet: ContainerConfiguration diff --git a/samples/logging/custom-factory/Core_10/CustomFactory.sln b/samples/logging/custom-factory/Core_10/CustomFactory.sln deleted file mode 100644 index 2ae4371675b..00000000000 --- a/samples/logging/custom-factory/Core_10/CustomFactory.sln +++ /dev/null @@ -1,18 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.8.34408.163 -MinimumVisualStudioVersion = 15.0.26730.12 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/logging/custom-factory/Core_10/Sample/ConsoleLog.cs b/samples/logging/custom-factory/Core_10/Sample/ConsoleLog.cs deleted file mode 100644 index 79f8df30c02..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/ConsoleLog.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System; -using NServiceBus.Logging; - -#region log -class ConsoleLog(string name, LogLevel level) : - ILog -{ - public bool IsDebugEnabled { get; } = LogLevel.Debug >= level; - public bool IsInfoEnabled { get; } = LogLevel.Info >= level; - public bool IsWarnEnabled { get; } = LogLevel.Warn >= level; - public bool IsErrorEnabled { get; } = LogLevel.Error >= level; - public bool IsFatalEnabled { get; } = LogLevel.Fatal >= level; - - void Write(string level, string message, Exception exception) - { - Console.WriteLine($"{name}. {level}. {message}. Exception: {exception}"); - } - - void Write(string level, string message) - { - Console.WriteLine($"{name}. {level}. {message}."); - } - - void Write(string level, string format, params object[] args) - { - format = $"{name}. {level}. {format}"; - Console.WriteLine(format, args); - } - - public void Debug(string message) - { - if (IsDebugEnabled) - { - Write("Debug", message); - } - } - - public void Debug(string message, Exception exception) - { - if (IsDebugEnabled) - { - Write("Debug", message, exception); - } - } - - public void DebugFormat(string format, params object[] args) - { - if (IsDebugEnabled) - { - Write("Debug", format, args); - } - } - // Other log methods excluded for brevity - #endregion - public void Info(string message) - { - if (IsInfoEnabled) - { - Write("Info", message); - } - } - - public void Info(string message, Exception exception) - { - if (IsInfoEnabled) - { - Write("Info", message, exception); - } - } - - public void InfoFormat(string format, params object[] args) - { - if (IsInfoEnabled) - { - Write("Info", format, args); - } - } - - public void Warn(string message) - { - if (IsWarnEnabled) - { - Write("Warn", message); - } - } - - public void Warn(string message, Exception exception) - { - if (IsWarnEnabled) - { - Write("Warn", message, exception); - } - } - - public void WarnFormat(string format, params object[] args) - { - if (IsWarnEnabled) - { - Write("Warn", format, args); - } - } - - public void Error(string message) - { - if (IsErrorEnabled) - { - Write("Error", message); - } - } - - public void Error(string message, Exception exception) - { - if (IsErrorEnabled) - { - Write("Error", message, exception); - } - } - - public void ErrorFormat(string format, params object[] args) - { - if (IsErrorEnabled) - { - Write("Error", format, args); - } - } - - public void Fatal(string message) - { - if (IsFatalEnabled) - { - Write("Fatal", message); - } - } - - public void Fatal(string message, Exception exception) - { - if (IsFatalEnabled) - { - Write("Fatal", message, exception); - } - } - - public void FatalFormat(string format, params object[] args) - { - if (IsFatalEnabled) - { - Write("Fatal", format, args); - } - } - -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerDefinition.cs b/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerDefinition.cs deleted file mode 100644 index c672c7786f2..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerDefinition.cs +++ /dev/null @@ -1,16 +0,0 @@ -#region definition -using NServiceBus.Logging; - -class ConsoleLoggerDefinition : - LoggingFactoryDefinition -{ - LogLevel level = LogLevel.Info; - - public void Level(LogLevel level) - { - this.level = level; - } - - protected override ILoggerFactory GetLoggingFactory() => new ConsoleLoggerFactory(level); -} -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerFactory.cs b/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerFactory.cs deleted file mode 100644 index 002ee1751a1..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/ConsoleLoggerFactory.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using NServiceBus.Logging; -#region factory - -class ConsoleLoggerFactory(LogLevel level) : - ILoggerFactory -{ - public ILog GetLogger(Type type) => GetLogger(type.FullName); - public ILog GetLogger(string name) => new ConsoleLog(name, level); -} - -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/MyHandler.cs b/samples/logging/custom-factory/Core_10/Sample/MyHandler.cs deleted file mode 100644 index 34baa0f4d9f..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/MyHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using NServiceBus; -using NServiceBus.Logging; - -public class MyHandler : - IHandleMessages -{ - static ILog log = LogManager.GetLogger(); - - public Task Handle(MyMessage message, IMessageHandlerContext context) - { - log.Info("Hello from MyHandler"); - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/MyMessage.cs b/samples/logging/custom-factory/Core_10/Sample/MyMessage.cs deleted file mode 100644 index 254edf5d23a..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/MyMessage.cs +++ /dev/null @@ -1,3 +0,0 @@ -using NServiceBus; - -public class MyMessage : IMessage; \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/Program.cs b/samples/logging/custom-factory/Core_10/Sample/Program.cs deleted file mode 100644 index e8c3fcd658c..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/Program.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using NServiceBus; -using NServiceBus.Logging; - -var host = Host.CreateDefaultBuilder(args) - .UseConsoleLifetime() - .UseNServiceBus(_ => - { - #region ConfigureLogging - var loggerDefinition = LogManager.Use(); - loggerDefinition.Level(LogLevel.Info); - #endregion - var endpointConfiguration = new EndpointConfiguration("Samples.Logging.CustomFactory"); - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(); - - return endpointConfiguration; - }) - .Build(); - -await host.StartAsync(); - -var endpointInstance = host.Services.GetRequiredService(); -await endpointInstance.SendLocal(new MyMessage()); - -Console.WriteLine("Press any key to exit"); -Console.ReadKey(); - -await host.StopAsync(); \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/Sample/Sample.csproj b/samples/logging/custom-factory/Core_10/Sample/Sample.csproj deleted file mode 100644 index 5468658e82c..00000000000 --- a/samples/logging/custom-factory/Core_10/Sample/Sample.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - net10.0 - Exe - 14.0 - - - - - - \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_10/prerelease.txt b/samples/logging/custom-factory/Core_10/prerelease.txt deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/samples/logging/custom-factory/Core_8/CustomFactory.sln b/samples/logging/custom-factory/Core_8/CustomFactory.sln deleted file mode 100644 index 50055481477..00000000000 --- a/samples/logging/custom-factory/Core_8/CustomFactory.sln +++ /dev/null @@ -1,19 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29728.190 -MinimumVisualStudioVersion = 15.0.26730.12 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/logging/custom-factory/Core_8/Sample/ConsoleLog.cs b/samples/logging/custom-factory/Core_8/Sample/ConsoleLog.cs deleted file mode 100644 index befadfac665..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/ConsoleLog.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System; -using NServiceBus.Logging; - -#region log -class ConsoleLog : - ILog -{ - string name; - public bool IsDebugEnabled { get; } - public bool IsInfoEnabled { get; } - public bool IsWarnEnabled { get; } - public bool IsErrorEnabled { get; } - public bool IsFatalEnabled { get; } - - public ConsoleLog(string name, LogLevel level) - { - this.name = name; - IsDebugEnabled = LogLevel.Debug >= level; - IsInfoEnabled = LogLevel.Info >= level; - IsWarnEnabled = LogLevel.Warn >= level; - IsErrorEnabled = LogLevel.Error >= level; - IsFatalEnabled = LogLevel.Fatal >= level; - } - - void Write(string level, string message, Exception exception) - { - Console.WriteLine($"{name}. {level}. {message}. Exception: {exception}"); - } - void Write(string level, string message) - { - Console.WriteLine($"{name}. {level}. {message}."); - } - - void Write(string level, string format, params object[] args) - { - format = $"{name}. {level}. {format}"; - Console.WriteLine(format, args); - } - - public void Debug(string message) - { - if (IsDebugEnabled) - { - Write("Debug", message); - } - } - - public void Debug(string message, Exception exception) - { - if (IsDebugEnabled) - { - Write("Debug", message, exception); - } - } - - public void DebugFormat(string format, params object[] args) - { - if (IsDebugEnabled) - { - Write("Debug", format, args); - } - } - // Other log methods excluded for brevity - #endregion - public void Info(string message) - { - if (IsInfoEnabled) - { - Write("Info", message); - } - } - - public void Info(string message, Exception exception) - { - if (IsInfoEnabled) - { - Write("Info", message, exception); - } - } - - public void InfoFormat(string format, params object[] args) - { - if (IsInfoEnabled) - { - Write("Info", format, args); - } - } - - public void Warn(string message) - { - if (IsWarnEnabled) - { - Write("Warn", message); - } - } - - public void Warn(string message, Exception exception) - { - if (IsWarnEnabled) - { - Write("Warn", message, exception); - } - } - - public void WarnFormat(string format, params object[] args) - { - if (IsWarnEnabled) - { - Write("Warn", format, args); - } - } - - public void Error(string message) - { - if (IsErrorEnabled) - { - Write("Error", message); - } - } - - public void Error(string message, Exception exception) - { - if (IsErrorEnabled) - { - Write("Error", message, exception); - } - } - - public void ErrorFormat(string format, params object[] args) - { - if (IsErrorEnabled) - { - Write("Error", format, args); - } - } - - public void Fatal(string message) - { - if (IsFatalEnabled) - { - Write("Fatal", message); - } - } - - public void Fatal(string message, Exception exception) - { - if (IsFatalEnabled) - { - Write("Fatal", message, exception); - } - } - - public void FatalFormat(string format, params object[] args) - { - if (IsFatalEnabled) - { - Write("Fatal", format, args); - } - } - -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerDefinition.cs b/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerDefinition.cs deleted file mode 100644 index d81b867746d..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerDefinition.cs +++ /dev/null @@ -1,19 +0,0 @@ -#region definition -using NServiceBus.Logging; - -class ConsoleLoggerDefinition : - LoggingFactoryDefinition -{ - LogLevel level = LogLevel.Info; - - public void Level(LogLevel level) - { - this.level = level; - } - - protected override ILoggerFactory GetLoggingFactory() - { - return new ConsoleLoggerFactory(level); - } -} -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerFactory.cs b/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerFactory.cs deleted file mode 100644 index 1f84e67075d..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/ConsoleLoggerFactory.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using NServiceBus.Logging; -#region factory - -class ConsoleLoggerFactory : - ILoggerFactory -{ - LogLevel level; - - public ConsoleLoggerFactory(LogLevel level) - { - this.level = level; - } - - public ILog GetLogger(Type type) - { - return GetLogger(type.FullName); - } - - public ILog GetLogger(string name) - { - return new ConsoleLog(name, level); - } -} - -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_8/Sample/MyHandler.cs b/samples/logging/custom-factory/Core_8/Sample/MyHandler.cs deleted file mode 100644 index 34baa0f4d9f..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/MyHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using NServiceBus; -using NServiceBus.Logging; - -public class MyHandler : - IHandleMessages -{ - static ILog log = LogManager.GetLogger(); - - public Task Handle(MyMessage message, IMessageHandlerContext context) - { - log.Info("Hello from MyHandler"); - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_8/Sample/MyMessage.cs b/samples/logging/custom-factory/Core_8/Sample/MyMessage.cs deleted file mode 100644 index 65952daa4c9..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/MyMessage.cs +++ /dev/null @@ -1,6 +0,0 @@ -using NServiceBus; - -public class MyMessage : - IMessage -{ -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_8/Sample/Program.cs b/samples/logging/custom-factory/Core_8/Sample/Program.cs deleted file mode 100644 index 8624cea6b8c..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/Program.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Threading.Tasks; -using NServiceBus; -using NServiceBus.Logging; - -class Program -{ - static async Task Main() - { - Console.Title = "CustomFactory"; - #region ConfigureLogging - - var loggerDefinition = LogManager.Use(); - - // optionally set the log level in code or read from app.config - loggerDefinition.Level(LogLevel.Info); - - // logging configuration should occur prior to endpoint configuration - var endpointConfiguration = new EndpointConfiguration("Samples.Logging.CustomFactory"); - - #endregion - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(); - - var endpointInstance = await Endpoint.Start(endpointConfiguration); - var myMessage = new MyMessage(); - await endpointInstance.SendLocal(myMessage); - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - await endpointInstance.Stop(); - } -} diff --git a/samples/logging/custom-factory/Core_8/Sample/Sample.csproj b/samples/logging/custom-factory/Core_8/Sample/Sample.csproj deleted file mode 100644 index cd288b0a577..00000000000 --- a/samples/logging/custom-factory/Core_8/Sample/Sample.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - net9.0;net8.0;net48 - Exe - 12.0 - - - - - \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/CustomFactory.sln b/samples/logging/custom-factory/Core_9/CustomFactory.sln deleted file mode 100644 index 2ae4371675b..00000000000 --- a/samples/logging/custom-factory/Core_9/CustomFactory.sln +++ /dev/null @@ -1,18 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.8.34408.163 -MinimumVisualStudioVersion = 15.0.26730.12 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/samples/logging/custom-factory/Core_9/Sample/ConsoleLog.cs b/samples/logging/custom-factory/Core_9/Sample/ConsoleLog.cs deleted file mode 100644 index befadfac665..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/ConsoleLog.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System; -using NServiceBus.Logging; - -#region log -class ConsoleLog : - ILog -{ - string name; - public bool IsDebugEnabled { get; } - public bool IsInfoEnabled { get; } - public bool IsWarnEnabled { get; } - public bool IsErrorEnabled { get; } - public bool IsFatalEnabled { get; } - - public ConsoleLog(string name, LogLevel level) - { - this.name = name; - IsDebugEnabled = LogLevel.Debug >= level; - IsInfoEnabled = LogLevel.Info >= level; - IsWarnEnabled = LogLevel.Warn >= level; - IsErrorEnabled = LogLevel.Error >= level; - IsFatalEnabled = LogLevel.Fatal >= level; - } - - void Write(string level, string message, Exception exception) - { - Console.WriteLine($"{name}. {level}. {message}. Exception: {exception}"); - } - void Write(string level, string message) - { - Console.WriteLine($"{name}. {level}. {message}."); - } - - void Write(string level, string format, params object[] args) - { - format = $"{name}. {level}. {format}"; - Console.WriteLine(format, args); - } - - public void Debug(string message) - { - if (IsDebugEnabled) - { - Write("Debug", message); - } - } - - public void Debug(string message, Exception exception) - { - if (IsDebugEnabled) - { - Write("Debug", message, exception); - } - } - - public void DebugFormat(string format, params object[] args) - { - if (IsDebugEnabled) - { - Write("Debug", format, args); - } - } - // Other log methods excluded for brevity - #endregion - public void Info(string message) - { - if (IsInfoEnabled) - { - Write("Info", message); - } - } - - public void Info(string message, Exception exception) - { - if (IsInfoEnabled) - { - Write("Info", message, exception); - } - } - - public void InfoFormat(string format, params object[] args) - { - if (IsInfoEnabled) - { - Write("Info", format, args); - } - } - - public void Warn(string message) - { - if (IsWarnEnabled) - { - Write("Warn", message); - } - } - - public void Warn(string message, Exception exception) - { - if (IsWarnEnabled) - { - Write("Warn", message, exception); - } - } - - public void WarnFormat(string format, params object[] args) - { - if (IsWarnEnabled) - { - Write("Warn", format, args); - } - } - - public void Error(string message) - { - if (IsErrorEnabled) - { - Write("Error", message); - } - } - - public void Error(string message, Exception exception) - { - if (IsErrorEnabled) - { - Write("Error", message, exception); - } - } - - public void ErrorFormat(string format, params object[] args) - { - if (IsErrorEnabled) - { - Write("Error", format, args); - } - } - - public void Fatal(string message) - { - if (IsFatalEnabled) - { - Write("Fatal", message); - } - } - - public void Fatal(string message, Exception exception) - { - if (IsFatalEnabled) - { - Write("Fatal", message, exception); - } - } - - public void FatalFormat(string format, params object[] args) - { - if (IsFatalEnabled) - { - Write("Fatal", format, args); - } - } - -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerDefinition.cs b/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerDefinition.cs deleted file mode 100644 index d81b867746d..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerDefinition.cs +++ /dev/null @@ -1,19 +0,0 @@ -#region definition -using NServiceBus.Logging; - -class ConsoleLoggerDefinition : - LoggingFactoryDefinition -{ - LogLevel level = LogLevel.Info; - - public void Level(LogLevel level) - { - this.level = level; - } - - protected override ILoggerFactory GetLoggingFactory() - { - return new ConsoleLoggerFactory(level); - } -} -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerFactory.cs b/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerFactory.cs deleted file mode 100644 index 1f84e67075d..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/ConsoleLoggerFactory.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using NServiceBus.Logging; -#region factory - -class ConsoleLoggerFactory : - ILoggerFactory -{ - LogLevel level; - - public ConsoleLoggerFactory(LogLevel level) - { - this.level = level; - } - - public ILog GetLogger(Type type) - { - return GetLogger(type.FullName); - } - - public ILog GetLogger(string name) - { - return new ConsoleLog(name, level); - } -} - -#endregion \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/Sample/MyHandler.cs b/samples/logging/custom-factory/Core_9/Sample/MyHandler.cs deleted file mode 100644 index 34baa0f4d9f..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/MyHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using NServiceBus; -using NServiceBus.Logging; - -public class MyHandler : - IHandleMessages -{ - static ILog log = LogManager.GetLogger(); - - public Task Handle(MyMessage message, IMessageHandlerContext context) - { - log.Info("Hello from MyHandler"); - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/Sample/MyMessage.cs b/samples/logging/custom-factory/Core_9/Sample/MyMessage.cs deleted file mode 100644 index 254edf5d23a..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/MyMessage.cs +++ /dev/null @@ -1,3 +0,0 @@ -using NServiceBus; - -public class MyMessage : IMessage; \ No newline at end of file diff --git a/samples/logging/custom-factory/Core_9/Sample/Program.cs b/samples/logging/custom-factory/Core_9/Sample/Program.cs deleted file mode 100644 index 8624cea6b8c..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/Program.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Threading.Tasks; -using NServiceBus; -using NServiceBus.Logging; - -class Program -{ - static async Task Main() - { - Console.Title = "CustomFactory"; - #region ConfigureLogging - - var loggerDefinition = LogManager.Use(); - - // optionally set the log level in code or read from app.config - loggerDefinition.Level(LogLevel.Info); - - // logging configuration should occur prior to endpoint configuration - var endpointConfiguration = new EndpointConfiguration("Samples.Logging.CustomFactory"); - - #endregion - endpointConfiguration.UseSerialization(); - endpointConfiguration.UseTransport(); - - var endpointInstance = await Endpoint.Start(endpointConfiguration); - var myMessage = new MyMessage(); - await endpointInstance.SendLocal(myMessage); - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - await endpointInstance.Stop(); - } -} diff --git a/samples/logging/custom-factory/Core_9/Sample/Sample.csproj b/samples/logging/custom-factory/Core_9/Sample/Sample.csproj deleted file mode 100644 index 839a535ac53..00000000000 --- a/samples/logging/custom-factory/Core_9/Sample/Sample.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - net9.0;net8.0 - Exe - 12.0 - - - - - \ No newline at end of file diff --git a/samples/logging/custom-factory/sample.md b/samples/logging/custom-factory/sample.md deleted file mode 100644 index 517843cbd6d..00000000000 --- a/samples/logging/custom-factory/sample.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -title: Custom Logger Factory -summary: Illustrates a custom implementation of a logging factory. -reviewed: 2024-01-11 -component: Core -related: - - nservicebus/logging ---- - -## Introduction - -Illustrates a custom implementation of a logging factory. For simplicity, this sample writes all log messages to the console. - -> [!NOTE] -> The approach of creating a custom logging factory should not be required in the development of most business applications. This API is designed for routing NServiceBus log messages to a third-party logging library. To gain more control over logging targets it is recommended to leverage one of these logging libraries. - -It is also possible to see full implementations of logging factories by looking at the code for the other logging libraries. - -* [Log4Net integration](/nservicebus/logging/log4net.md) -* [NLog integration](/nservicebus/logging/nlog.md) -* [CommonLogging integration](/nservicebus/logging/common-logging.md) - -## Logging Definition - -To build a custom logging factory, create a class derived from `LoggingFactoryDefinition`. - -snippet: definition - -## Logger Factory - -The `LoggingFactoryDefinition` then returns an instance of `ILoggerFactory` - -snippet: factory - -Note that the `ConsoleLoggerFactory` can optionally expose extra configuration, as is illustrated in this case by a custom `LogLevel()` method. - -## Log - -The logger factory then returns an `ILog` instance. - -snippet: log - -The implementation of `ILog` handles writing the entries and the log filtering. - -### Enabling Logging - -snippet: ConfigureLogging - -include: verifyLoggingSample