我使用這個代碼在5月C#MonoTouch的應用程序中保存的日誌:共享衝突爲我的日誌管理
public static void writeExeption(string message){
string path= StorageClass .LogsPath ;
string filepath= Path.Combine (path ,"Log.txt");
if(!File.Exists (filepath)){
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine ("--------------------------------------------------------------------------" +
"--------------------");
sw.WriteLine("blahblah...");
sw.WriteLine ("--------------------------------------------------------------------------" +
"--------------------");
}
}
using (StreamWriter w = File.AppendText(filepath))
{
Log(message , w);
}
}
public static void Log(string logMessage, TextWriter w)
{
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", logMessage);
w.WriteLine ("--------------------------------------------------------------------------" +
"--------------------");
}
但在應用我得到這個錯誤:
Sharing violation on path 'File Path'
** 1 **。在哪一行?你到目前爲止嘗試過什麼? ** 2。**可能重複http://stackoverflow.com/questions/11541244/sharing-violation-on-path-error-c-sharp以及http://stackoverflow.com/questions/14313303/file -sharing-violation-occurrence-occur-creation-of-file –
line「w.WriteLine(」:{0}「,logMessage);」 。我試過http://stackoverflow.com/questions/3817477/simultaneous-read-write-a-file-in-c-sharp和http://social.msdn.microsoft.com/Forums/en-US/vblanguage/線程/ 52f4f8fb-a434-4660-9806-3a30e3bbffb2但不適合我。我也看到你現在發送的鏈接。 –
如果您想在.net中執行日誌記錄,則應該使用一個輕量級框架(如log4net或Nlog),它們將爲您處理所有文件訪問和線程同步。不要試圖自己寫。 – Peter