views:

1642

answers:

6

I am running a Web Service in C# .NET 3.5. I want to log various calls. However since many users are hitting the same functions at once it is difficult to tell which log call belongs to which.

In a C++ life we used a thread Id. What is the equivalent in C#?

I have tried

System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
proc.Id;

However this gets me the same Id each time.

+3  A: 

Mayhaps Thread.CurrentThread.ManagedThreadId

Rytmis
A: 

You can use Thread.CurrentThread.ManagedThreadId.

Vojislav Stojkovic
A: 

I would use

System.Threading.Thread.CurrentThread.ManagedThreadId

Also, since this is a web service, if the clients are coming from different IP Addresses, you could log that too which may well help with logging/diagnostics.

CraigTP
A: 

If you use some logging frameworks such as log4net, you will notice that thread ID is logged automatically for you.

It is very easy to start with.

Lex Li
A: 

Take a look at ASP.NET Health Monitoring. See ASP.NET Health Monitoring Overview. The events that it logs by default contain more information than you probably want:

Log Name: Application Source: ASP.NET 2.0.50727.0 Date: 2/12/2009 12:57:14 Event ID: 1309 Task Category: Web Event Level: Warning Keywords: Classic User: N/A Computer: Laptop Description: Event code: 3005 Event message: An unhandled exception has occurred. Event time: 2/12/2009 12:57:14 Event time (UTC): 2/12/2009 17:57:14 Event ID: b08e9d3be7364690a660254dc0a29b6d Event sequence: 5 Event occurrence: 4 Event detail code: 0

Application information: Application domain: bba4cebc-1-128789349683310000 Trust level: Full Application Virtual Path: / Application Path: C:\Users\John Saunders\Documents\MVP\projects\WebServices\GeneralSoln\WebService1\ Machine name: LAPTOP

Process information: Process ID: 11560 Process name: WebDev.WebServer.exe Account name: Laptop\John Saunders

Exception information: Exception type: MissingMethodException Exception message: Method not found: 'Void System.ServiceModel.Diagnostics.EventLogger.UnsafeLogEvent(System.Diagnostics.TraceEventType, System.ServiceModel.Diagnostics.EventLogCategory, System.ServiceModel.Diagnostics.EventLogEventId, Boolean, System.String[])'.

Request information: Request URL: http://localhost:5305/Enumerations.asmx Request path: /Enumerations.asmx User host address: 127.0.0.1 User:
Is authenticated: False Authentication Type:
Thread account name: Laptop\John Saunders

Thread information: Thread ID: 4 Thread account name: Laptop\John Saunders Is impersonating: False Stack trace: at System.Runtime.CompilerServices.RuntimeHelpers.PrepareDelegate(Delegate d) at System.AppDomain.add_UnhandledException(UnhandledExceptionEventHandler value) at System.ServiceModel.ServiceHostingEnvironment.HookADUnhandledExceptionEvent() at System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized() at System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state) at System.ServiceModel.PartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state) at System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized() at System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Custom event details:

Event Xml:

John Saunders
+1  A: 

If you use MS Ent Lib and their logger you can use the Tracer class to get some nice logging:

Example

public Output MyServiceMethod(Input input, string transactionId)
{
    using(new Tracer("MyServiceMethod: " + transactionId))
    {
        ... stuff ...
        return output;
    }
}

You can even nest Tracers. All logging within the using will have the string you give Tracer constructor as an extended property.

Tommy