如何将范围应用于NServiceBus行为中间件中的日志

问题描述 投票:0回答:1

在以下示例中,我将范围应用于NServiceBus Behavior中间件中的ILogger。不幸的是,此作用域不会在后续的next()调用中持续存在。我如何在IHandleMessages处理程序中获取范围以保留消息?

public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{            
    var correlationId = context.Message.Headers[Headers.CorrelationId] ?? context.Message.MessageId;

    using (_logger.BeginScope(correlationId))
    {
        await next().ConfigureAwait(false);                
    }            
}
c# asp.net-core nservicebus
1个回答
1
投票

我实现了类似于使用传入消息中的已知标头设置日志记录上下文的操作,以便我的日志包含上下文信息。

有两个步骤:

  1. 处理传入消息
    • 如果消息包含已知的标题,则:
      • 将标头作为名称/值存储在管道上下文中(或与行为相关的任何上下文,假设您可以访问此类上下文)
      • 将相关上下文设置到记录器中(我目前正在使用nLog)
  2. 处理外发邮件
    • 检索管道上下文并将其存储到传出邮件头中

实施例:

public class IncomingBehavior :  Behavior<IIncomingLogicalMessageContext>
    {
        public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
        {            
            context.StoreHeaderValueToPipelineContext(KnownHeader.MyHeader);
            NLog.MappedDiagnosticsLogicalContext.Clear();
            context.StoreHeaderToNLogContext(KnownHeader.MyHeader); 
            return next();
        }
    }

public class OutgoingBehavior : Behavior<IOutgoingLogicalMessageContext>
    {
        public override Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
        {
            context.StorePipelineContextToOutgoingMessageHeader(KnownHeader.MyHeader); 
            return next();
        }
    }

public static class IMessageProcessingExtensions
    {
        public static IMessageProcessingContext StoreHeaderValueToPipelineContext(this IMessageProcessingContext context,
            string headerName)
        {
            if (context.MessageHeaders.TryGetValue(headerName, out string headerValue))
            {
                context.Extensions.Set(headerName, headerValue);
            }

            return context;
        }

        public static IOutgoingLogicalMessageContext StorePipelineContextToOutgoingMessageHeader
        (this IOutgoingLogicalMessageContext context,
            string headerName)
        {
            if (context.Extensions.TryGet(headerName, out string headerValue))
            {
                context.Headers[headerName] = headerValue;
            }

            return context;
        }

        public static IMessageProcessingContext StoreHeaderToNLogContext(this IMessageProcessingContext context,
            string headerName)
        {
            if (context.MessageHeaders.TryGetValue(headerName, out string headerValue))
            {
                NLog.MappedDiagnosticsLogicalContext.Set(headerName, headerValue);
            }

            return context;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.