zoukankan      html  css  js  c++  java
  • 利用日志记录所有LINQ的增,删,改解决方案

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

            }
        }
  • 相关阅读:
    Notepad++编写Markdown
    解决Unable to create new native thread
    Outlook2016 新装进阶操作指南
    卷积神经网络
    反向传播算法
    神经网络的基本组成
    cs231n课程索引
    快速入门特征工程
    快速入门Sklearn
    快速入门Matplotlib
  • 原文地址:https://www.cnblogs.com/nuaalfm/p/1327485.html
Copyright © 2011-2022 走看看