首页 新闻 会员 周边 捐助

EFCore 如何记录Insert 和Update Sql语句

0
悬赏园豆:10 [已解决问题] 解决于 2019-01-23 15:50

版本: netcore 1.1.12, EfCore 1.12

实现代码:

 Builder.UseLoggerFactory(Utility.Logging.SqlLoggerFactory.GetLoggerFactory());

 

结果只打印出查询的语句

2018-08-01 14:27:25,308 [12] TRACKED: True
(QueryContext queryContext) => IEnumerable<VoteOption> _ShapedQuery(
queryContext: queryContext,
shaperCommandContext: SelectExpression:
SELECT `m`.`Id`, `m`.`ActivityTaskId`, `m`.`ConsumePropId`, `m`.`CreateTime`, `m`.`OptionDescription`, `m`.`OptionImgUrl`, `m`.`OptionName`, `m`.`Sort`, `m`.`UpdateTime`, `m`.`VoteNum`
FROM `VoteOption` AS `m`
WHERE `m`.`ActivityTaskId` = @__ActivityTaskId_0
ORDER BY `m`.`Id` DESC
LIMIT @__p_2 OFFSET @__p_1
,
shaper: UnbufferedEntityShaper<VoteOption>
)


2018-08-01 14:27:25,308 [12] Opening connection to database 'actd' on server 'localhost'.

2018-08-01 14:27:25,309 [12] Closing connection to database 'actd' on server 'localhost'.

求教 如何打印出 insert 和update语句

jzblive的主页 jzblive | 菜鸟二级 | 园豆:428
提问于:2018-08-01 14:38
< >
分享
最佳答案
0

可以修改 EF Core 的日志级别,会自动产生日志

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning",
      "Microsoft.EntityFrameworkCore": "Information"
    }
  }
}
收获园豆:10
dudu | 高人七级 |园豆:29817 | 2018-08-01 21:19

还是没有update 和insert语句

public class SqlLoggerFactory
    {
        private static readonly object LockObj = new object();

        private static ILoggerFactory _LoggerFactory;

        public static ILoggerFactory GetLoggerFactory()
        {
            if (_LoggerFactory == null)
            {
                lock (LockObj)
                {
                    if (_LoggerFactory == null)
                    {
                        _LoggerFactory = new LoggerFactory();
                        _LoggerFactory.AddProvider(new SqlLoggerProvider());
                        _LoggerFactory.AddProvider(new ConsoleLoggerProvider((_,__) => { return true; }, false));
                        //_LoggerFactory.AddProvider(new DebugLoggerProvider((_, __) => { return true; }));
                    }
                }
            }
            return _LoggerFactory;
        }
    }
builder.UseMySQL(ActivityConnectionString, options =>
                            options.MigrationsAssembly(migrationsAssembly));
                builder.EnableSensitiveDataLogging();
                builder.UseLoggerFactory(Utility.Logging.SqlLoggerFactory.GetLoggerFactory());

 

dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[3]
Opening connection to database 'act' on server 'localhost'.
dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[4]
Closing connection to database 'act' on server 'localhost'.
dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[3]
Opening connection to database 'act' on server 'localhost'.
dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[5]
Beginning transaction with isolation level 'Unspecified'.
dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[6]
Committing transaction.
dbug: MySql.Data.EntityFrameworkCore.MySQLServerConnection[4]
Closing connection to database 'act' on server 'localhost'.
dbug: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action method **.**.Controllers.VoteOptionController.Save , returned result Microsoft.AspNetCore.Mvc.JsonResult.
info: Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor[1]
Executing JsonResult, writing value { code = 0, msg = 编辑成功, data = }.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action *.*.*.*Controller.Save in 334.5086ms

jzblive | 园豆:428 (菜鸟二级) | 2018-08-02 10:31

@jzblive: 去掉 builder.UseLoggerFactory(Utility.Logging.SqlLoggerFactory.GetLoggerFactory());

dudu | 园豆:29817 (高人七级) | 2018-08-02 10:37

@dudu: 没有效果。

查询语句的Sql日志可以正常打印出来。

在执行update或 Insert时,只打印出了数据库连接开关的信息,这应该与消息LogLevel没有关系吧。

jzblive | 园豆:428 (菜鸟二级) | 2018-08-02 11:10

@jzblive: 是 update 或 insert 时数据库中的数据已生效,只是没有生成日志吗?

dudu | 园豆:29817 (高人七级) | 2018-08-02 11:30

@dudu: 对的

jzblive | 园豆:428 (菜鸟二级) | 2018-08-02 11:38

@jzblive: 问题应该与MySQL有关,SQL Server应该没这个问题

dudu | 园豆:29817 (高人七级) | 2018-08-02 11:49

@dudu: 好的 回头我去试一下 sqlserver的,谢谢你

jzblive | 园豆:428 (菜鸟二级) | 2018-08-02 12:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册