2016-07-19 14 views
2

共通のトレースIDを使用します。私は次のコードを使用しています。共通トレースIDの使用方法は?

public void method1(){ 
     using (new Tracer(Guid.NewGuid().ToString())) 
     { 
      //my code 
     } 
    } 
    public void method2(){ 
     using (new Tracer(Guid.NewGuid().ToString())) 
     { 
      //my code 
     } 
    } 

ここで、guidは私のトレースIDです。しかし、すべてのメソッド呼び出しに対して異なるトレースIDが生成されます。私はそれをユニークなものにしておきたい。これを達成する方法? (注:私はmethod1、method2を別のクライアントから呼び出します)

答えて

1

クラス名に関する情報や.NET <の情報を入手する必要がある場合は、StackFrameを使用してください。 StackFrameでオーバーヘッドが発生します。クラス名を取得する必要がなく、.NET> = 4.5を使用する場合は、ここにsolutionがあります。それはCaller Informationを使用します。 :

namespace Tracer 
{ 
    using System; 
    using System.Runtime.CompilerServices; 
    sealed class CallerInfoTracer : IDisposable 
    { 
     private readonly string _message; 
     private readonly string _memberName; 
     private readonly string _sourceFilePath; 
     private readonly int _lineNumber; 

     private bool _disposed; 

     public CallerInfoTracer(string message, [CallerMemberName] string memberName = "", 
      [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int lineNumber = 0) 
     { 
      _message = message; 
      _memberName = memberName; 
      _sourceFilePath = sourceFilePath; 
      _lineNumber = lineNumber; 
     } 
     public void Dispose() 
     { 
      if (_disposed) return; 

      Console.WriteLine("Message: {0}", _message); 
      Console.WriteLine("MemberName: {0}", _memberName); 
      Console.WriteLine("SourceFilePath: {0}", _sourceFilePath); 
      Console.WriteLine("LineNumber: {0}", _lineNumber); 
      _disposed = true; 
     } 
    } 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      Method1(); 
      Method2(); 
     } 
     public static void Method1() 
     { 
      using (var tracer = new CallerInfoTracer("Desc1")) { } 
     } 
     public static void Method2() 
     { 
      using (var tracer = new CallerInfoTracer("Desc2")) { } 
     } 
    } 
} 
+0

単一スコープを使用できますか? – Karthikeyan

+0

@カーティケヤン、どういう意味ですか? –

+0

すべての方法で使用するのは良くありません。だから、それを避けるための方法はありますか? – Karthikeyan

関連する問題