版本: 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语句
可以修改 EF Core 的日志级别,会自动产生日志
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
}
}
}
还是没有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: 去掉 builder.UseLoggerFactory(Utility.Logging.SqlLoggerFactory.GetLoggerFactory());
@dudu: 没有效果。
查询语句的Sql日志可以正常打印出来。
在执行update或 Insert时,只打印出了数据库连接开关的信息,这应该与消息LogLevel没有关系吧。
@jzblive: 是 update 或 insert 时数据库中的数据已生效,只是没有生成日志吗?
@dudu: 对的
@jzblive: 问题应该与MySQL有关,SQL Server应该没这个问题
@dudu: 好的 回头我去试一下 sqlserver的,谢谢你