Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / LogHandler-v5.cs
Created July 16, 2019 08:44
NServiceBus - Log all incoming messages handler
using System;
using NServiceBus;
using NServiceBus.Logging;
class LogHandler : IHandleMessages<object>
{
static readonly ILog Log = LogManager.GetLogger<LogHandler>();
public IBus Bus { get; set; }
@ramonsmits
ramonsmits / OriginatingMachineLatencyBehavior-v7.cs
Created July 18, 2019 13:22
NServiceBus Machine Latency Windows Performance Counter Sample
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Pipeline;
class OriginatingMachineLatencyBehavior : Behavior<IIncomingPhysicalMessageContext>
{
ConcurrentDictionary<string, (PerformanceCounter Counter, PerformanceCounter Base)> machineCounters = new ConcurrentDictionary<string, (PerformanceCounter,PerformanceCounter)>();
// https://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net
public static class SizeFormatter
{
static String StrFormatByteSize (long length, IFormatProvider provider)
{
const int kb = 1024;
string[] suffix = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (length == 0) return "0" + suffix[0];
var bytes = Math.Abs(length);
var index = (int)Math.Floor(Math.Log(bytes, kb));
@ramonsmits
ramonsmits / LogMessageHeadersMutator.cs
Last active February 17, 2020 14:27
NServiceBus - Log message headers for all incoming messages via mutator
using System.Text;
using System.Threading.Tasks;
using NServiceBus.Logging;
using NServiceBus.MessageMutator;
class LogMessageHeadersMutator : IMutateIncomingTransportMessages
{
static readonly ILog Log = LogManager.GetLogger<LogMessageHeadersMutator>();
public Task MutateIncoming(MutateIncomingTransportMessageContext context)
@ramonsmits
ramonsmits / BehaviorContextBagFlow.cs
Created September 27, 2019 08:42
NServiceBus - Sample that shows the flow of context bag extension data from the incoming to outgoing context
// LogManager.Use<DefaultFactory>().Level(LogLevel.Debug);
// var pipeline = endpointConfiguration.Pipeline;
// pipeline.Register(new IN(), nameof(IN));
// pipeline.Register(new OUT(), nameof(OUT));
class IN : Behavior<IIncomingLogicalMessageContext>
{
public const string Key = "count";
static long count;
static readonly ILog Log = LogManager.GetLogger<IN>();
@ramonsmits
ramonsmits / CustomOptionsWrapper.cs
Last active November 18, 2019 16:06
Wrap IEndpointInstance to set custom options on all sends with NServiceBus 7
using System;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus;
public class CustomOptionsWrapper : IEndpointInstance
{
readonly IEndpointInstance innerInstance;
readonly IPrincipalAccessor principalAccessor;
// Usage: endpointConfiguration.Pipeline.Register(typeof(EnforceTransactionScopePromotionBehavior), nameof(EnforceTransactionScopePromotionBehavior));
public class EnforceTransactionScopePromotionBehavior : IBehavior<IIncomingPhysicalMessageContext, IIncomingPhysicalMessageContext>
{
public Task Invoke(IIncomingPhysicalMessageContext context, Func<IIncomingPhysicalMessageContext, Task> next)
{
var transaction = System.Transactions.Transaction.Current;
if(transaction==null) throw new InvalidOperationException($"No ambient TransactionScope active, disable {nameof(EnforceTransactionScopePromotionBehavior)} behavior");
System.Transactions.TransactionInterop.GetTransmitterPropagationToken(transaction);
return next(context);
}
static class Jitter
{
static readonly Random Random = new Random();
public static TimeSpan Next(int count, TimeSpan first)
{
double jitter;
lock (Random) jitter = Random.NextDouble(); // Needed to make random threadsafe
return TimeSpan.FromTicks((long) (first.Ticks * Math.Pow(2, count + jitter)));
}
@ramonsmits
ramonsmits / EndpointInstance.sql
Created December 16, 2019 21:23
Retrieves MSMQ endpoint instance data stored in a SQL Server instead from the file system.
CREATE TABLE [dbo].[EndpointInstance]
(
[EndpointName] VARCHAR(128) NOT NULL,
[Discriminator] VARCHAR(128) NULL DEFAULT NULL,
[Machine] VARCHAR(256) NOT NULL
)
for /R %%X in (*.csv) do "c:\Program Files\7-Zip\7z.exe" a -tgzip -sdel -mx9 "%%X.gz" "%%X"