zoukankan      html  css  js  c++  java
  • Entity Framework Logging and Intercepting Database Operations (EF6 Onwards)

    1.自定义日志

        public class LogHelper
        {
            public static void DbInfo(string str)
            {
                string className = "方法名";
                string path = "/DbInfo.txt";
                if (string.IsNullOrEmpty(path))
                {
                    path = "/DbInfo.txt";
                }
                string oriPath = HttpContext.Current.Request.MapPath("/LogFile");
                if (!Directory.Exists(oriPath))
                {
                    Directory.CreateDirectory(oriPath);
                }
                try
                {
                    FileStream fs = new FileStream(HttpContext.Current.Request.MapPath("/LogFile" + path), FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    var host = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority;
                    sw.WriteLine(string.Format("domain={0},className={1},time={2}", host, className, DateTime.Now));
                    sw.WriteLine(str);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                }
                catch (Exception)
                {
                   
                }
       
            }
    }
    View Code

    2. Set DbContext.Database.Log property 

        public class QxunDbContext : DbContext 
        {
            public QxunDbContext()
                : base("server=localhost;uid=sa;pwd=6665508a;database=Qxun;")
            {
                this.Database.Log = LogHelper.DbInfo;
            }
            
            public DbSet<Department> Departments { get; set; }
            public DbSet<Course> Courses { get; set; } 
        }
    View Code

    3.Show LogInfo

    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    已于 2017/7/8 星期六 上午 11:22:10 +08:00
     打开了连接
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    已于 2017/7/8 星期六 上午 11:22:10 +08:00
     启动了事务
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    INSERT [dbo].[Courses]([Title], [Credits])
    VALUES (@0, @1)
    SELECT [CourseID]
    FROM [dbo].[Courses]
    WHERE @@ROWCOUNT > 0 AND [CourseID] = scope_identity()
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    
    
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    -- @0: 'Title' (Type = String, Size = -1)
    
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    -- @1: '1' (Type = Int32)
    
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    -- 正在 2017/7/8 星期六 上午 11:22:10 +08:00
     执行
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    -- 已在 0 毫秒内完成,结果为: SqlDataReader
    
    domain=http://localhost:50462,className=方法名,time=2017/7/8 星期六 上午 11:22:10
    LogInfo
  • 相关阅读:
    Java实现 LeetCode 740 删除与获得点数(递推 || 动态规划?打家劫舍Ⅳ)
    Python oct() 函数
    Python hex() 函数
    Python ord() 函数
    Python unichr() 函数
    Python chr() 函数
    arm,asic,dsp,fpga,mcu,soc各自的特点
    摄像头标定技术
    自主泊车技术分析
    畸变的单目摄像机标定
  • 原文地址:https://www.cnblogs.com/liandy0906/p/7136226.html
Copyright © 2011-2022 走看看