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);
                }

            }
        }
  • 相关阅读:
    ASCII对应码表-键值(完整版)
    node.js中使用路由方法
    关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)
    js中 !==和 !=的区别是什么
    spring 请求参数和路径变量
    PowerShell因为在此系统中禁止执行脚本解决方法
    SQL server 2008数据库的备份与还原(亲测,效果良好)注意采用单用户模式呀
    webpack-dev-server提示css模块解析失败,但已经装了css-loader
    webpack集成vue单文件模式的很多坑(研究了1个星期)
    npm全局模块卸载及默认安装目录修改方法
  • 原文地址:https://www.cnblogs.com/nuaalfm/p/1327485.html
Copyright © 2011-2022 走看看