我在C#.NET 3.5中运行Web服务。我想记录各种电话。但是,由于许多用户同时使用相同的功能,因此很难判断哪个日志调用属于哪个。
在C ++生活中,我们使用了一个线程Id。 C#中的等价物是什么?
我试过了
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
proc.Id;
但是每次都会得到相同的ID。
答案 0 :(得分:3)
可能是Thread.CurrentThread.ManagedThreadId
答案 1 :(得分:1)
如果你使用MS Ent Lib及其记录器,你可以使用Tracer类来获得一些不错的记录:
实施例
public Output MyServiceMethod(Input input, string transactionId)
{
using(new Tracer("MyServiceMethod: " + transactionId))
{
... stuff ...
return output;
}
}
您甚至可以嵌套追踪器。使用中的所有日志记录都将使用您为Tracer构造函数作为扩展属性的字符串。
答案 2 :(得分:0)
您可以使用Thread.CurrentThread.ManagedThreadId
。
答案 3 :(得分:0)
我会用
System.Threading.Thread.CurrentThread.ManagedThreadId
此外,由于这是一个Web服务,如果客户端来自不同的IP地址,您也可以记录它,这可能有助于记录/诊断。
答案 4 :(得分:0)
如果你使用一些日志框架,例如log4net,你会发现自动为你记录了线程ID。
这很容易入手。
答案 5 :(得分:0)
看一下ASP.NET Health Monitoring。见ASP.NET Health Monitoring Overview。默认情况下,它记录的事件包含的信息比您可能需要的信息多:
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: