ABP增加记录EFCore生成数据库脚本日志到新的txt文件
2023年7月18日发(作者:)
ABP增加记录EFCore⽣成数据库脚本⽇志到新的txt⽂件由于EFCore并没直接⽣成脚本到txt⽂件,故⽽⾃⼰画了点时间把实现记录下来,⽅便给⼤家参考。0.安装,我这⾥是2.1.1版本。1.新建⼀个EFCoreDatabaseCmdLog在abp的Core层下,using ency;using g;namespace { public static class EFCoreDatabaseCmdLog { public static ILogger Logger { get; private set; } static EFCoreDatabaseCmdLog() { Logger = stered(typeof(ILoggerFactory)) e().Create(typeof(EFCoreDatabaseCmdLog)) : ce; } public static void Debug(string logContent) { (logContent); } }} 2.在FrameworkCore层EntityFrameworkCore⽂件夹下新增2个⽂件与g g;using System;namespace FrameworkCore{ public class EFLogger : ILogger { private readonly string categoryName; public EFLogger(string categoryName) => ryName = categoryName; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { //ef core执⾏数据库查询时的categoryName为d,⽇志级别为Information if (categoryName == "d" && logLevel == ation) { var logContent = formatter(state, exception); (logContent);//xxxx 需要替换成⾃⼰的项⽬名字 } } public IDisposable BeginScope(TState state) => null; }} EFLoggerProviderusing g;namespace FrameworkCore{ public class EFLoggerProvider : ILoggerProvider { public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName); public void Dispose() { } }}3.修改⽂件如下,我这⾥⽤的是mysql,请酌情参考。using ;using FrameworkCore;using g;namespace FrameworkCore{ public static class SensorBrokerDbContextConfigurer {
public static void Configure(DbContextOptionsBuilder builder, string connectionString) { var loggerFactory = new LoggerFactory(); vider(new EFLoggerProvider()); ql(connectionString); gerFactory(loggerFactory); SensitiveDataLogging(); } public static void Configure(DbContextOptionsBuilder builder, DbConnection connection) { var loggerFactory = new LoggerFactory(); vider(new EFLoggerProvider()); ql(connection); gerFactory(loggerFactory); SensitiveDataLogging(); } }}4.修改配置在log4net节点中增加如下配置 这样所有的数据库脚本⽇志都就会到中。
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689650193a273771.html
评论列表(0条)