2009-02-12 4 views
0

J'exécute un service Web en C# .NET 3.5. Je veux enregistrer divers appels. Cependant, étant donné que de nombreux utilisateurs utilisent les mêmes fonctions à la fois, il est difficile de savoir à quel journal appartient l'appel.Sur un service Web C#, puis-je trouver un ID de thread unique ou un ID à des fins de journalisation?

Dans une vie C++, nous avons utilisé un Id de thread. Quel est l'équivalent en C#?

J'ai essayé

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

Cependant, cela me fait le même identifiant chaque fois.

Répondre

1

Si vous utilisez MS Ent Lib et leur enregistreur, vous pouvez utiliser la classe Tracer pour obtenir une exploitation forestière agréable:

Exemple

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

Vous pouvez même imbriquer des Tracers. Toute connexion dans l'utilisation aura la chaîne que vous donnerez au constructeur Tracer comme une propriété étendue.

3

Mayhaps Thread.CurrentThread.ManagedThreadId

0

Vous pouvez utiliser Thread.CurrentThread.ManagedThreadId.

0

J'utilise

System.Threading.Thread.CurrentThread.ManagedThreadId 

De plus, puisque c'est un service Web, si les clients viennent de différentes adresses IP, vous pouvez vous connecter que trop ce qui pourrait bien aider à l'exploitation forestière/diagnostic.

0

Si vous utilisez des cadres de journalisation tels que log4net, vous remarquerez que l'ID de thread est automatiquement enregistré pour vous.

Il est très facile de commencer avec.

0

Jetez un coup d'œil à ASP.NET Health Monitoring. Voir ASP.NET Health Monitoring Overview. Les événements que les journaux par défaut contiennent plus d'informations que vous voulez sans doute:

 
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: 
Questions connexes