Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Snippets/Core/Core_10/Core_10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
<PackageReference Include="NServiceBus" Version="10.2.0-alpha.5" />
<PackageReference Include="NServiceBus" Version="10.2.0-alpha.12" />
<PackageReference Include="NServiceBus.Callbacks" Version="6.*" />
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.*" />
<PackageReference Include="NServiceBus.Encryption.MessageProperty" Version="6.*" />
Expand Down
21 changes: 21 additions & 0 deletions Snippets/Core/Core_10/Logging/ModernLogging.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace Core.Logging;

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using NServiceBus.Logging;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

public class MyMessage : IMessage { }

Expand Down Expand Up @@ -49,3 +53,20 @@ void ConfigureHost()
}
#endregion
}

#region UsingEndpointLoggingScope
public class MyBackgroundService(
ILogger<MyBackgroundService> logger,
EndpointLoggingScope endpointScope) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (logger.BeginEndpointScope(endpointScope))
{
logger.LogInformation("Doing background work");
}

await Task.CompletedTask;
}
}
#endregion
3 changes: 1 addition & 2 deletions Snippets/Core/Core_10/Recoverability/Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public async override Task Invoke(IRecoverabilityContext context, Func<Task> nex
{
if (context.RecoverabilityAction is MoveToError errorAction)
{
var message = context.FailedMessage;
var bodyUrl = await storage.StoreBody(message.MessageId, message.Body);
var bodyUrl = await storage.StoreBody(context.MessageId, context.Body);

context.Metadata["body-url"] = bodyUrl;

Expand Down
2 changes: 1 addition & 1 deletion nservicebus/logging/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Logging
summary: NServiceBus logging
reviewed: 2026-04-27
reviewed: 2026-05-19
component: Core
isLearningPath: true
redirects:
Expand Down
10 changes: 10 additions & 0 deletions nservicebus/logging/index_modern_core_[10,).partial.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ snippet: UsingLoggerMessageSourceGenerator

> [!WARNING]
> The NServiceBus-specific logging APIs described in the next section below are still functional but produce obsolete warnings starting in version 10.2 and will be removed in version 12. Use the `Microsoft.Extensions.Logging` patterns shown earlier for new development.

## Enriching logs outside the message pipeline

Logs written from inside the message-processing pipeline automatically include the endpoint name and identifier as structured log properties. Logs written from code outside the pipeline, such as a hosted background task or a custom timer callback, do not include this context by default.

To attach endpoint context to those logs, resolve the `EndpointLoggingScope` from the DI container and use the `BeginEndpointScope` extension method:

snippet: UsingEndpointLoggingScope

The `EndpointLoggingScope` instance is scoped to the endpoint and carries the endpoint name and identifier with it. The `BeginEndpointScope` extension method detects when the call occurs inside the message-processing pipeline and returns a no-op scope. This avoids duplicate scope entries when the pattern is used from message handlers.
Loading