ASP.NETで、Traceをログファイルに出力 – プログラミング – Home

ASP.NETで、Traceをログファ...
 
通知
すべてクリア

ASP.NETで、Traceをログファイルに出力


クレオ
 クレオ
(@クレオ)
ゲスト
結合: 18年前
投稿: 40
Topic starter  

ほぼ↓と同様なのですが
http://msdn2.microsoft.com/ja-
JP/library/system.diagnostics.textwritertracelistener.close.aspx

以下のようにコーディングしました。
しかし、ファイルは出力されませんし、catch句にの入ってきません。
ただおかしいのが、ブレークポイントを置いてみるとトレースすると
Listeners.Addのところで、例外がおきたみたいにそこまでしか実行されません。(catch
には入ってこない)
こんな現象ご存知でしょうか?

protected void Page_Load(object sender, EventArgs e)
{

String myFileName = D:\\Work\\TestFile.txt;
if (!File.Exists(myFileName))
{
Stream myFile = File.Create(myFileName);
myFile.Close(); // ←追加
}

try
{
// Assign output file to output stream.
StreamWriter myOutputWriter;
myOutputWriter = File.AppendText(myFileName);

/* Create a new text writer using the output stream, and
* add it to the trace listeners. */
TextWriterTraceListener myTextListener = new
TextWriterTraceListener(myOutputWriter);
System.Diagnostics.Trace.Listeners.Add(myTextListener);

// Write output to the file.
System.Diagnostics.Trace.WriteLine(Test output);

// Flush and close the output stream.
System.Diagnostics.Trace.Flush();
System.Diagnostics.Trace.Close();

}
catch(Exception e2)
{
string str = e2.StackTrace;
}
}


引用解決済
トピックタグ
クレオ
 クレオ
(@クレオ)
ゲスト
結合: 18年前
投稿: 40
Topic starter  

シンプルに以下のように変えてみました。Page_Loadにこれを書いてます。

using (FileStream objStream = new FileStream(C:\\Temp\\TestFile.txt,
FileMode.OpenOrCreate))
{
TextWriterTraceListener myTextListener = new TextWriterTraceListener
(objStream);
System.Diagnostics.Trace.Listeners.Add(myTextListener);
System.Diagnostics.Trace.WriteLine(Test output);
System.Diagnostics.Trace.Flush();
System.Diagnostics.Trace.Close();
}

結局、空ファイルができてしまうのは変わりません。

ただ、このソースをコンソールアプリから実行するとちゃんとファイルができます。

コンソールアプリと、ASPアプリだと何か違うのでしょうか?
最初、書き込む権利がないのかと思っていたのですが、空ファイルができるところから
書き込む権利はあるようです。


返信引用

返信する

投稿者名

投稿者メールアドレス

タイトル *

プレビュー 0リビジョン 保存しました
共有:
タイトルとURLをコピーしました