利用日誌記錄所有LINQ的增,刪,改解決方案

來源:互聯網
上載者:User

在項目初期部署的時候,如果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);
            }

        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.