其他分享
首页 > 其他分享> > EF SaveChanges重写加入日志

EF SaveChanges重写加入日志

作者:互联网

 public override int SaveChanges()
        {
            var entries = ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged).GroupBy(x => new { x.State, x.Entity.GetType().Name }).Select(x => new { x.Key.State, x.Key.Name, List = x.ToList() });
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore// 发现循环引用时跳过序列化
            };
            foreach (var item in entries)
            {
                        Logger.WriteOperationlog($"\r\n Operation {item.State} Date:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n Entity:{item.Name}\r\n Data:{JsonConvert.SerializeObject(item.List.Select(x => x.Entity).ToList(), settings)}", "Default");

            }

            return base.SaveChanges();
        }

 

标签:Name,EF,Entity,item,State,new,日志,SaveChanges
来源: https://www.cnblogs.com/330774495qq/p/12855884.html