Skip to content

Instantly share code, notes, and snippets.

@jamesmanning
Created March 18, 2012 04:08
Show Gist options
  • Save jamesmanning/2068772 to your computer and use it in GitHub Desktop.
Save jamesmanning/2068772 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
class Program
{
private static ILog s_logger = LogManager.GetLogger("Program");
static void Main(string[] args)
{
BasicConfigurator.Configure();
var task1 = DoSomeWork("chunk 1");
var task2 = DoSomeWork("chunk 2");
var task3 = DoSomeWork("chunk 3");
Task.WaitAll(task1, task2, task3);
}
static async Task DoSomeWork(string chunkName)
{
using (new SomeDisposable(chunkName))
//using (NDC.Push(chunkName))
using (log4net.GlobalContext.Stacks["NDC"].Push(chunkName))
//using (log4net.LogicalThreadContext.Stacks["NDC"].Push(chunkName))
//using (log4net.ThreadContext.Stacks["NDC"].Push(chunkName))
{
s_logger.Info("Starting to do work");
await Task.Delay(5000);
s_logger.Info("Finishing work");
}
}
}
public class SomeDisposable : IDisposable
{
private static ILog s_logger = LogManager.GetLogger("SomeDisposable");
private string m_chunkName;
public SomeDisposable(string chunkName)
{
m_chunkName = chunkName;
s_logger.InfoFormat("Created disposable for chunk {0} on thread {1}!", m_chunkName, Thread.CurrentThread.ManagedThreadId);
}
void IDisposable.Dispose()
{
s_logger.InfoFormat("Disposed disposable for chunk {0} on thread {1}!", m_chunkName, Thread.CurrentThread.ManagedThreadId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment