在項目初期部署的時候,如果bug沒有被排除乾淨,但代碼部署到客戶機上了,那麼調試bug會是個問題,一般我們都會在這段時間把日誌開啟,在日誌中將操作過程記錄到日誌中。為了將所有的增,刪,改的操作都記錄下來,我們會加入一個資料內容的分布類,然後重寫SubmitChanges方法,以下是我的解決方案:
public partial class YourDataContext : System.Data.Linq.DataContext
{
public override void SubmitChanges(ConflictMode failureMode)
{
//記錄日誌(每天一個檔案,記錄所有更改sql,日誌會存在第一個盤的log檔案夾下)
if (ConfigurationManager.AppSettings["IsTraceLinqLog"].ToString().ToLower()
== "true")
{
string directory = Path.Combine(Directory.GetLogicalDrives().First(), "log");
Directory.CreateDirectory(directory);
string logFile = Path.Combine(directory,
"log" + DateTime.Now.ToShortDateString() + ".txt");
using (StreamWriter w = File.AppendText(logFile))
{
w.WriteLine("發生時間:{0}", DateTime.Now.ToString());
w.WriteLine("日誌內容為:");
this.Log = w;
try
{
base.SubmitChanges(failureMode);
}
catch (Exception e)
{
w.WriteLine(e.Message);
throw;
}
w.WriteLine("--------------------------------------------------------------");
}
}
else
{
base.SubmitChanges(failureMode);
}
}
}