DefaultTraceListener クラス

定義

トレースの動作と既定の出力メソッドを提供します。

public ref class DefaultTraceListener : System::Diagnostics::TraceListener
public class DefaultTraceListener : System.Diagnostics.TraceListener
[System.Runtime.InteropServices.ComVisible(false)]
public class DefaultTraceListener : System.Diagnostics.TraceListener
type DefaultTraceListener = class
    inherit TraceListener
[<System.Runtime.InteropServices.ComVisible(false)>]
type DefaultTraceListener = class
    inherit TraceListener
Public Class DefaultTraceListener
Inherits TraceListener
継承
DefaultTraceListener
継承
DefaultTraceListener
属性

次のコード例では、確率と統計で使用される値である二項係数を計算します。 この例では、 を DefaultTraceListener 使用して結果をトレースし、エラーをログに記録します。 新 DefaultTraceListenerしい を作成し、コレクションに Trace.Listeners 追加し、 プロパティを LogFileName コマンド ライン引数で指定されたログ ファイルに設定します。

入力パラメーターの処理中にエラーが検出された場合、または関数が例外を CalcBinomial スローした場合、メソッドはログを Fail 記録してエラー メッセージを表示します。 プロパティが の AssertUiEnabled 場合、 falseエラー メッセージもコンソールに書き込まれます。 結果が正常に計算されると、 Write(String) メソッドと WriteLine(String) メソッドは結果をログ ファイルに書き込みます。

FailWrite、および WriteLine の各メソッドを使用すると、トレース情報は にのみDefaultTraceListener書き込まれます。 コレクション内のすべてのリスナーにトレース情報を Trace.Listeners 書き込むには、 クラスの FailWrite、および WriteLine メソッドを Trace 使用します。

using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class Binomial
{

    // args(0) is the number of possibilities for binomial coefficients.
    // args(1) is the file specification for the trace log file.
    public static void Main(string[] args)
    {

        decimal possibilities;
        decimal iter;

        // Remove the original default trace listener.
        Trace.Listeners.RemoveAt(0);

        // Create and add a new default trace listener.
        DefaultTraceListener defaultListener;
        defaultListener = new DefaultTraceListener();
        Trace.Listeners.Add(defaultListener);

        // Assign the log file specification from the command line, if entered.
        if (args.Length>=2)
        {
            defaultListener.LogFileName = args[1];
        }

        // Validate the number of possibilities argument.
        if (args.Length>=1)

            // Verify that the argument is a number within the correct range.
        {
            try
            {
                const decimal MAX_POSSIBILITIES = 99;
                possibilities = Decimal.Parse(args[0]);
                if (possibilities<0||possibilities>MAX_POSSIBILITIES)
                {
                    throw new Exception(String.Format("The number of possibilities must " +
                        "be in the range 0..{0}.", MAX_POSSIBILITIES));
                }
            }
            catch(Exception ex)
            {
                string failMessage = String.Format("\"{0}\" " +
                    "is not a valid number of possibilities.", args[0]);
                defaultListener.Fail(failMessage, ex.Message);
                if (!defaultListener.AssertUiEnabled)
                {
                    Console.WriteLine(failMessage+ "\n" +ex.Message);
                }
                return;
            }
        }
        else
        {
            // Report that the required argument is not present.
            const string ENTER_PARAM = "Enter the number of " +
                      "possibilities as a command line argument.";
            defaultListener.Fail(ENTER_PARAM);
            if (!defaultListener.AssertUiEnabled)
            {
                Console.WriteLine(ENTER_PARAM);
            }
            return;
        }

        for(iter=0; iter<=possibilities; iter++)
        {
            decimal result;
            string binomial;

            // Compute the next binomial coefficient and handle all exceptions.
            try
            {
                result = CalcBinomial(possibilities, iter);
            }
            catch(Exception ex)
            {
                string failMessage = String.Format("An exception was raised when " +
                    "calculating Binomial( {0}, {1} ).", possibilities, iter);
                defaultListener.Fail(failMessage, ex.Message);
                if (!defaultListener.AssertUiEnabled)
                {
                    Console.WriteLine(failMessage+ "\n" +ex.Message);
                }
                return;
            }

            // Format the trace and console output.
            binomial = String.Format("Binomial( {0}, {1} ) = ", possibilities, iter);
            defaultListener.Write(binomial);
            defaultListener.WriteLine(result.ToString());
            Console.WriteLine("{0} {1}", binomial, result);
        }
    }

    public static decimal CalcBinomial(decimal possibilities, decimal outcomes)
    {

        // Calculate a binomial coefficient, and minimize the chance of overflow.
        decimal result = 1;
        decimal iter;
        for(iter=1; iter<=possibilities-outcomes; iter++)
        {
            result *= outcomes+iter;
            result /= iter;
        }
        return result;
    }
}
Imports System.Diagnostics

Module Binomial

    ' args(0) is the number of possibilities for binomial coefficients.
    ' args(1) is the file specification for the trace log file.
    Sub Main(ByVal args() As String)

        Dim possibilities As Decimal
        Dim iter As Decimal

        ' Remove the original default trace listener.
        Trace.Listeners.RemoveAt(0)

        ' Create and add a new default trace listener.
        Dim defaultListener As DefaultTraceListener
        defaultListener = New DefaultTraceListener
        Trace.Listeners.Add(defaultListener)

        ' Assign the log file specification from the command line, if entered.
        If args.Length >= 2 Then
            defaultListener.LogFileName = args(1)
        End If

        ' Validate the number of possibilities argument.
        If args.Length >= 1 Then

            ' Verify that the argument is a number within the correct range.
            Try
                Const MAX_POSSIBILITIES As Decimal = 99
                possibilities = Decimal.Parse(args(0))
                If possibilities < 0 Or possibilities > MAX_POSSIBILITIES Then
                    Throw New Exception( _
                        String.Format("The number of possibilities must " & _
                            "be in the range 0..{0}.", MAX_POSSIBILITIES))
                End If
            Catch ex As Exception
                Dim failMessage As String = String.Format("""{0}"" " & _
                    "is not a valid number of possibilities.", args(0))
                defaultListener.Fail(failMessage, ex.Message)
                If Not defaultListener.AssertUiEnabled Then
                    Console.WriteLine(failMessage & vbCrLf & ex.Message)
                End If
                Return
            End Try
        Else
            ' Report that the required argument is not present.
            Const ENTER_PARAM As String = "Enter the number of " & _
                "possibilities as a command line argument."
            defaultListener.Fail(ENTER_PARAM)
            If Not defaultListener.AssertUiEnabled Then
                Console.WriteLine(ENTER_PARAM)
            End If
            Return
        End If

        For iter = 0 To possibilities
            Dim result As Decimal
            Dim binomial As String

            ' Compute the next binomial coefficient and handle all exceptions.
            Try
                result = CalcBinomial(possibilities, iter)
            Catch ex As Exception
                Dim failMessage As String = String.Format( _
                        "An exception was raised when " & _
                        "calculating Binomial( {0}, {1} ).", _
                        possibilities, iter)
                defaultListener.Fail(failmessage, ex.Message)
                If Not defaultListener.AssertUiEnabled Then
                    Console.WriteLine(failMessage & vbCrLf & ex.Message)
                End If
                Return
            End Try

            ' Format the trace and console output.
            binomial = String.Format("Binomial( {0}, {1} ) = ", _
                            possibilities, iter)
            defaultListener.Write(binomial)
            defaultListener.WriteLine(result.ToString)
            Console.WriteLine("{0} {1}", binomial, result)
        Next
    End Sub

    Function CalcBinomial(ByVal possibilities As Decimal, _
                        ByVal outcomes As Decimal) As Decimal

        ' Calculate a binomial coefficient, and minimize the chance of overflow.
        Dim result As Decimal = 1
        Dim iter As Decimal
        For iter = 1 To possibilities - outcomes
            result *= outcomes + iter
            result /= iter
        Next
        Return result
    End Function
End Module

注釈

このクラスのインスタンスは、 コレクションと Trace.Listeners コレクションに自動的にDebug.Listeners追加されます。 2 つ目 DefaultTraceListener を明示的に追加すると、デバッガー出力ウィンドウでメッセージが重複し、アサートのメッセージ ボックスが重複します。

既定では、 Write メソッドと WriteLine メソッドは、Win32 OutputDebugString 関数と メソッドにメッセージを Debugger.Log 出力します。

既定では、 メソッドは Fail 、アプリケーションがユーザー インターフェイス モードで実行されているときにメッセージ ボックスを表示します。また、 を使用して WriteLineメッセージを出力します。

Note

メソッド呼び出しと メソッド呼び出しFailのメッセージ ボックスAssertの表示は、 DefaultTraceListenerの存在によって異なります。 DefaultTraceListenerがコレクション内にないListeners場合、メッセージ ボックスは表示されません。 をDefaultTraceListener削除するには、 プロパティ (System.Diagnostics.Trace.Listeners.Clear()) で Clear メソッドをListeners呼び出します。 .NET Frameworkアプリの場合は、アプリの<構成ファイルで clear> 要素<remove> 要素を使用することもできます。

トレース リスナーを使用するには、トレースまたはデバッグを有効にする必要があります。 次の構文はコンパイラ固有です。 C# または Visual Basic 以外のコンパイラを使用する場合は、コンパイラのドキュメントを参照してください。

  • C# でデバッグを有効にするには、コードの /d:DEBUG コンパイル時にコンパイラ コマンド ラインに フラグを追加するか、ファイルの先頭に を追加 #define DEBUG します。 Visual Basic で、 フラグを /d:DEBUG=True コンパイラ コマンド ラインに追加します。

  • C# でトレースを有効にするには、コードの /d:TRACE コンパイル時にコンパイラ コマンド ラインに フラグを追加するか、ファイルの先頭に を追加 #define TRACE します。 Visual Basic で、 フラグを /d:TRACE=True コンパイラ コマンド ラインに追加します。

.NET Framework アプリの場合は、アプリケーションの名前に対応する構成ファイルを編集することで、トレース リスナーを追加できます。 このファイル内では、リスナーの追加、その型の設定、パラメーターの設定、リスナーの削除、またはアプリケーションによって以前に設定されたすべてのリスナーのクリアを行うことができます。 構成ファイルは、次の例のように書式設定する必要があります。

<configuration>  
<system.diagnostics>  
  <trace autoflush="false" indentsize="4">  
    <listeners>  
      <remove name="Default" />  
      <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\myListener.log" />  
    </listeners>  
  </trace>  
</system.diagnostics>  
</configuration>  

コンストラクター

DefaultTraceListener()

"Default" を DefaultTraceListener プロパティ値として使用して、Name クラスの新しいインスタンスを初期化します。

プロパティ

AssertUiEnabled

アプリケーションがユーザー インターフェイス モードで実行されているかどうかを示す値を取得または設定します。

Attributes

アプリケーション構成ファイルに定義されているトレース リスナーのカスタム属性を取得します。

(継承元 TraceListener)
Filter

トレース リスナーのトレース フィルターを取得または設定します。

(継承元 TraceListener)
IndentLevel

インデント レベルを取得または設定します。

(継承元 TraceListener)
IndentSize

1 つのインデントに含まれるスペースの数を取得または設定します。

(継承元 TraceListener)
IsThreadSafe

トレース リスナーがスレッド セーフかどうかを示す値を取得します。

(継承元 TraceListener)
LogFileName

トレース メッセージまたはデバッグ メッセージを書き込むログ ファイルの名前を取得または設定します。

Name

この TraceListener の名前を取得または設定します。

(継承元 TraceListener)
NeedIndent

出力にインデントを設定するかどうかを示す値を取得または設定します。

(継承元 TraceListener)
TraceOutputOptions

トレース出力オプションを取得または設定します。

(継承元 TraceListener)

メソッド

Close()

派生クラスでオーバーライドされた場合、出力ストリームを終了して、トレース出力またはデバッグ出力を受信しないようにします。

(継承元 TraceListener)
CreateObjRef(Type)

リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。

(継承元 MarshalByRefObject)
Dispose()

TraceListener によって使用されているすべてのリソースを解放します。

(継承元 TraceListener)
Dispose(Boolean)

TraceListener によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

(継承元 TraceListener)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Fail(String)

常に失敗するアサーションのメッセージとスタック トレースを出力または表示します。

Fail(String, String)

常に失敗するアサーションの詳細メッセージとスタック トレースを出力または表示します。

Flush()

派生クラスでオーバーライドされた場合、出力バッファーをフラッシュします。

(継承元 TraceListener)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetSupportedAttributes()

トレース リスナーによってサポートされるカスタム属性を取得します。

(継承元 TraceListener)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

トレース情報、データ オブジェクト、およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

トレース情報、データ オブジェクトの配列、およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32)

トレース情報およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

トレース情報、メッセージ、およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

トレース情報、オブジェクトの書式付き配列、およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
TraceTransfer(TraceEventCache, String, Int32, String, Guid)

トレース情報、メッセージ、関連するアクティビティ ID、およびイベント情報をリスナー固有の出力に書き込みます。

(継承元 TraceListener)
Write(Object)

オブジェクトの ToString() メソッドの値を、TraceListener クラスを実装した時に作成したリスナーに書き込みます。

(継承元 TraceListener)
Write(Object, String)

カテゴリ名と、オブジェクトの ToString() メソッドの値を、TraceListener クラスを実装するときに作成したリスナーに書き込みます。

(継承元 TraceListener)
Write(String)

出力を OutputDebugString 関数と Log(Int32, String, String) メソッドに書き込みます。

Write(String, String)

TraceListener クラスを実装した時に作成したリスナーにカテゴリ名とメッセージを書き込みます。

(継承元 TraceListener)
WriteIndent()

このクラスの実装時に作成したリスナーにインデントを書き込み、NeedIndent プロパティを false にリセットします。

(継承元 TraceListener)
WriteLine(Object)

TraceListener クラスの実装時に作成したリスナーにオブジェクトの ToString() メソッドの値と行終端記号を書き込みます。

(継承元 TraceListener)
WriteLine(Object, String)

TraceListener クラスの実装時に作成したリスナーにカテゴリ名、オブジェクトの ToString() メソッドの値、および行終端記号を書き込みます。

(継承元 TraceListener)
WriteLine(String)

出力を OutputDebugString 関数と Log(Int32, String, String) メソッドに書き込み、続けて復帰とライン フィード (\r\n) を書き込みます。

WriteLine(String, String)

TraceListener クラスの実装時に作成したリスナーにカテゴリ名、メッセージ、および行終端記号を書き込みます。

(継承元 TraceListener)

適用対象

スレッド セーフ

このクラスはスレッド セーフです。

こちらもご覧ください