Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net unique request id in ASP.NET

log4net 1.2.11.0

I am trying to get anything that will allow me to log with a unique value for each ASP.NET request.

I tried %thread, but threads appear to be reused.

I've tried %aspnet-request and %aspnet-session which don't have anything meaningful within them.

I looked at ThreadContext.Properties and LogicalThreadContext.Properties but they don't have anything in them either.

Anyone have a trick to get this done? I need the ability to pick a particular request's logs out of the log file.

like image 974
LiteWait Avatar asked Apr 05 '13 19:04

LiteWait


1 Answers

.asmx files will still call Application_BeginRequest event where you can store your unique GUID in HttpContext.Current.Items (using this collection avoid the odd problem when the request processing jumps threads). Only if you were using WCF (and also then it depends on configuration) this would not work.

If you would like not to touch the application itself, you can use HttpContext.Current.Timestamp that returns the time when the request processing started. Join that with your existing approach of getting the ID of the thread and you will get a unique value.

like image 110
Knaģis Avatar answered Sep 23 '22 09:09

Knaģis