Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / buffered.xml
Last active March 14, 2016 13:49
Log4net appender examples
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- Buffer 128 entries except with log level severity is WARN or higher. -->
<appender name="buffer" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="128" />
<appender-ref ref="console" />
<!--<lossy value="false" />-->
<!--<OnlyFixPartialEventData value="true"/>-->
<evaluator type="log4net.Core.LevelEvaluator">
@ramonsmits
ramonsmits / InitNServiceBusLog4net.cs
Created February 5, 2016 10:04
Use log4net with NServiceBus using log4net xml configuration in application configuration file
//
// Run this before anything NServiceBus related. If using
// the host, put this in the constructor of the endpoint
// configuration class.
//
log4net.Config.XmlConfigurator.Configure();
NServiceBus.Logging.LogManager.Use<NServiceBus.Log4Net.Log4NetFactory>();
@ramonsmits
ramonsmits / LogExtensions.cs
Last active February 18, 2016 08:39 — forked from lkaczanowski/ILogExtensions.cs
ILog extension for TRACE and VERBOSE log4net levels
using System;
using System.Globalization;
using log4net.Util;
namespace log4net
{
public static class LogExtentions
{
private static readonly Type ThisDeclaringType = typeof(LogExtentions);
@ramonsmits
ramonsmits / CustomerNotificationHandler.cs
Last active July 12, 2018 09:07
NServiceBus buffering saga example
public class CustomerNotificationHandler
: IHandleMessage<CustomerOrderNotification>
, IHandleMessage<CustomerOrderStatusNotification>
{
public IBus Bus {get;set;}
INotificationService GetClientForCustomer(string customerId)
{
return null; // Create a api client for your customer
@ramonsmits
ramonsmits / Program.cs
Created May 5, 2017 13:40
Global mutex preventing multiple instances
class Program
{
static Mutex SingleInstanceMutex = AcquireGlobalMutex("Receiver");
static Mutex AcquireGlobalMutex(string id, int durationInSeconds = 1)
{
var m = new Mutex(true, @"Global\" + id);
var maxWaitDuration = TimeSpan.FromSeconds(durationInSeconds);
if (!m.WaitOne(maxWaitDuration)) throw new InvalidOperationException("Failed to acquire mutex.");
return m;
@ramonsmits
ramonsmits / SendDoSomethingTask.cs
Last active July 5, 2017 12:55
NServiceBus scheduler alternative - NServiceBus V6 & NServiceBus.Host V7
public class SendDoSomethingTask : IWantToRunWhenEndpointStartsAndStops
{
ILog log = LogManager.GetLogger<SendDoSomethingTask>();
Task loop;
CancellationTokenSource cancellationTokenSource;
public Task Start(IMessageSession session)
{
cancellationTokenSource = new CancellationTokenSource();
loop = Task.Run(async () =>
@ramonsmits
ramonsmits / uptime.cs
Created July 7, 2017 14:41
Get Windows uptime with c#
var uptimeInSeconds = Stopwatch.GetTimestamp() / Stopwatch.Frequency;
var uptime = TimeSpan.FromSeconds(uptimeInSeconds);
Console.WriteLine(uptime);
@ramonsmits
ramonsmits / LogDispatchedMessageIdBehavior.cs
Created July 11, 2017 13:17
Log the message ID's from the messages that have been dispatched to the transport
using System;
using System.Threading.Tasks;
using NServiceBus.Logging;
using NServiceBus.Pipeline;
/// <code>
/// endpointConfiguration.Pipeline.Register(behavior: new LogDispatchedMessageIdBehavior(), description: LogDispatchedMessageIdBehavior.Description);
/// </code>
class LogDispatchedMessageIdBehavior : Behavior<IBatchDispatchContext>
{
@ramonsmits
ramonsmits / MetricsViaPerformanceCounters.cs
Created August 1, 2017 11:33
Metrics published via performance counter. Shows a simple way to create and/or update counters
using System;
using System.Diagnostics;
using System.Linq;
interface IMetrics
{
void UpdateDuration(Stopwatch value);
}
class MetricsViaPerformanceCounters : IDisposable, IMetrics
@ramonsmits
ramonsmits / ExponentialBackoffPolicy.cs
Last active October 10, 2017 20:54
NServiceBus custom recoverability policy - Exponential backoff with jitter
using System;
using NServiceBus;
using NServiceBus.Transport;
class ExponentialBackoffPolicy
{
static Random random = new Random();
public static RecoverabilityAction Process(RecoverabilityConfig config, ErrorContext context)
{