デバッグ情報をファイルに出力する

デバッグ情報をファイルに出力したい場合は
System.Diagnostics.Debug.Listenersコレクションに
出力先として追加したいファイルを追加します。

出力先の出力パネルが表示されていない場合は
VisualStudioのメニューより
【表示】→【出力】
を選択して表示してください。

debug_file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // デバッグ情報を出力するファイル名(実行ファイルと同じフォルダに置きます)
            string filename = AppDomain.CurrentDomain.BaseDirectory + "debug.txt";

            // 出力先としてテキストファイルを追加
            System.Diagnostics.TextWriterTraceListener texttrace = 
                new System.Diagnostics.TextWriterTraceListener(filename);
            System.Diagnostics.Debug.Listeners.Add(texttrace);

            // 何か適当に出力
            for (int i = 1; i <= 10; i++)
                System.Diagnostics.Debug.WriteLine(i + "行目");

            // 出力バッファをフラッシュし、キャッシュをちゃんと書き込む
            System.Diagnostics.Debug.Flush();

            // debug.txtファイルをメモ帳などで開いてみてください
            MessageBox.Show("debug.txtファイルをメモ帳などで開いてみてください");
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)