私はテキストファイルとしてログを書き込むプログラムを持っています。スレッドを呼び出してC言語のテキストファイルにログするより良い方法
namespace logging
{
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
public class log
{
private static string lpath;
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static void Info(string user, string info, string txt)
{
StreamWriter writer;
string str = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
.....
if (!File.Exists(path))
{
writer = new StreamWriter(path, true);
writer.WriteLine(str3 + " " + info + " => " + txt);
writer.Flush();
writer.Close();
}
else
{
writer = File.AppendText(path);
writer.Write(str3 + " " + info + " => " + txt + "\n");
writer.Flush();
writer.Close();
}
}
}
}
次に機能があります。インナー機能(BoardingPass_Print()
とBaggageTag_Print()
)の
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Thread thread_BP_Print = new Thread(BoardingPass_Print);
thread_BP_Print.Start();
// Simultaneously, do something on the main thread.
BaggageTag_Print();
bgw.RunWorkerAsync();
}
二つは同一のロギング機能を呼び出します。そのため、私の呼び出し元のスレッド法の
logging.log.Info(username, "Query Trace > ", sql);
、私はエラーに直面思う:
The process cannot access the file 'C:\Folder\2012-01-17-Name.txt' because it is being used by another process.
は、誰も私にこのようなエラーメッセージが直面することなく、テキストファイルへの糸と、ロギングと対話するためのよりよい解決策を与えてもらえますか?
すべての提案は本当に感謝しています。