Problems with ThreadContext and TransactionScope

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems with ThreadContext and TransactionScope

Ílson Bolzan
Has anyone tried to stack contexts and use Transaction scope at the same time?

I'm trying something like this:
using (log4net.ThreadContext.Stacks["contextLog"].Push("Saving Data"))
{
	log.Info("Starting transaction");
	using (var ts = new TransactionScope(TransactionScopeOption.Required))
	{
		log.Info("Inside Transaction");
	}
}

and I'm getting that result:

2015/42/26 13:42:10,841 INFO  [Saving Data] Starting transaction
2015/42/26 13:42:10,870 INFO  [(null)] Inside Transaction

I was expecting it to have [Saving Data] instead of [(null)]  on the second line.

It appears to loose access to the log4net ThreadContext Stack as soon as it enters a transaction.

Do you know how to avoid this?

Thank you so much.


Reply | Threaded
Open this post in threaded view
|

Re: Problems with ThreadContext and TransactionScope

Ron Grabowski
Do you get the same error when you use LogicalThreadContext?


From: Ílson Bolzan <[hidden email]>
To: Log4NET User <[hidden email]>
Sent: Wednesday, August 26, 2015 1:18 PM
Subject: Problems with ThreadContext and TransactionScope

Has anyone tried to stack contexts and use Transaction scope at the same time?

I'm trying something like this:
using (log4net.ThreadContext.Stacks["contextLog"].Push("Saving Data"))
{
	log.Info("Starting transaction");
	using (var ts = new TransactionScope(TransactionScopeOption.Required))
	{
		log.Info("Inside Transaction");
	}
}

and I'm getting that result:

2015/42/26 13:42:10,841 INFO  [Saving Data] Starting transaction
2015/42/26 13:42:10,870 INFO  [(null)] Inside Transaction

I was expecting it to have [Saving Data] instead of [(null)]  on the second line.

It appears to loose access to the log4net ThreadContext Stack as soon as it enters a transaction.

Do you know how to avoid this?

Thank you so much.




Reply | Threaded
Open this post in threaded view
|

Re: Problems with ThreadContext and TransactionScope

Ílson Bolzan
Hi Ron,

You were right about LogicalThreadContext. It worked.

Also, I was wrong about the problem. It has to do with System.Threading.Tasks and not with TransactionScope.

http://stackoverflow.com/questions/32233419/conflict-between-log4nets-threadcontext-and-task/

Thank you.

On Wed, Aug 26, 2015 at 8:43 PM, Ron Grabowski <[hidden email]> wrote:
Do you get the same error when you use LogicalThreadContext?


From: Ílson Bolzan <[hidden email]>
To: Log4NET User <[hidden email]>
Sent: Wednesday, August 26, 2015 1:18 PM
Subject: Problems with ThreadContext and TransactionScope

Has anyone tried to stack contexts and use Transaction scope at the same time?

I'm trying something like this:
using (log4net.ThreadContext.Stacks["contextLog"].Push("Saving Data"))
{
	log.Info("Starting transaction");
	using (var ts = new TransactionScope(TransactionScopeOption.Required))
	{
		log.Info("Inside Transaction");
	}
}

and I'm getting that result:

2015/42/26 13:42:10,841 INFO  [Saving Data] Starting transaction
2015/42/26 13:42:10,870 INFO  [(null)] Inside Transaction

I was expecting it to have [Saving Data] instead of [(null)]  on the second line.

It appears to loose access to the log4net ThreadContext Stack as soon as it enters a transaction.

Do you know how to avoid this?

Thank you so much.