logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject RE: Logging Thread Context Properties
Date Fri, 16 Jun 2006 04:20:51 GMT
Here's a failing test case for the problem you described. The
StringAppender has a value of "(null)" when it should have the string
representation of today's date.

A similiar test case that launches the thread via the ThreadPool:

 ThreadPool.QueueUserWorkItem(
  new WaitCallback(ExecuteBackgroundThreadPool), null);

also fails.

The TestThreadPropertiesPattern test from
log4net.Tests.Context.ThreadContextTest passes when the property is
added from the same thread.

In my ASP.Net applications, I store the user's identification
information during Application_AuthenticateRequest in the
ThreadContext(?) and all my appenders have access to it.

I'll look around some more...

[Test]
public void TestBackgroundThreadContextProperty()
{
 StringAppender stringAppender = new StringAppender();
 stringAppender.Layout = 
  new PatternLayout("%property{DateTimeTodayToString}");

 ILoggerRepository rep = 
  LogManager.CreateRepository("BackgroundThreadRepository");

 BasicConfigurator.Configure(rep, stringAppender);

 Thread thread = new Thread(new ThreadStart(ExecuteBackgroundThread));
 thread.Start();

 Thread.CurrentThread.Join(2000);
}

private void ExecuteBackgroundThread()
{
 ILog log = LogManager.GetLogger(
  "BackgroundThreadRepository", 
  "ExecuteBackgroundThread");
	
 log.Info("TestMessage");

 ThreadContext.Properties["DateTimeTodayToString"] = 
  DateTime.Today.ToString();

 Repository.Hierarchy.Hierarchy hierarchyLoggingRepository = 
  (Repository.Hierarchy.Hierarchy)log.Logger.Repository;
	
 StringAppender stringAppender = 
  (StringAppender)hierarchyLoggingRepository.Root.Appenders[0];

 Assert.AreEqual(DateTime.Today.ToString(),
stringAppender.GetString());
}

--- Wayne Bradney <Wayne.Bradney@WallStreetSystems.com> wrote:

> So, either I'm doing something so unbelievably stupid as to not
> warrant a
> response, or this is a real problem, right?
> 
>  
> 
> WMB
> 
>   _____  
> 
> From: Wayne Bradney [mailto:Wayne.Bradney@WallStreetSystems.com] 
> Sent: Wednesday, June 07, 2006 6:05 PM
> To: Log4NET User
> Subject: Logging Thread Context Properties
> 
>  
> 
> I must be missing something here... I'm trying to setup a thread
> property in
> my background thread such that my appender can inspect the property
> when an
> event is logged:
> 
>  
> 
> The thread does this:
> 
>  
> 
> log4net.ThreadContext.Properties["ThreadTaskDescription"] = "<some
> string>";
> 
> log.Info("Some message");
> 
>  
> 
>  
> 
> In the appender, when I look at the Properties property of the
> LoggingEvent
> ("Some message"), it never has any properties at all.
> 
>  
> 
> Am I doing this right?
> 
>  
> 
> WMB
> 
>  
> 
> 

Mime
View raw message