feat: 增加 SQL 语句执行输出

This commit is contained in:
Argo-Tianyi 2021-12-22 00:20:59 +08:00
parent 5eddf6ad24
commit fe36e7d888
1 changed files with 33 additions and 1 deletions

View File

@ -3,10 +3,16 @@ using BootstrapAdmin.DataAccess.PetaPoco.Services;
using BootstrapAdmin.Web.Core;
using BootstrapBlazor.Components;
using BootstrapBlazor.DataAcces.PetaPoco.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PetaPoco;
using PetaPoco.Providers;
using System.Collections.Specialized;
using System.Data.Common;
using System.Text;
namespace Microsoft.Extensions.DependencyInjection
{
@ -27,7 +33,33 @@ namespace Microsoft.Extensions.DependencyInjection
//TODO: 后期改造成自定适配
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
return new Database<SQLiteDatabaseProvider>(connString, new BootstrapAdminConventionMapper());
var db = new Database<SQLiteDatabaseProvider>(connString, new BootstrapAdminConventionMapper());
var logger = provider.GetRequiredService<ILogger<Database>>();
db.ExceptionThrown += (sender, e) =>
{
var message = e.Exception.Format(new NameValueCollection()
{
[nameof(db.LastCommand)] = db.LastCommand,
[nameof(db.LastArgs)] = string.Join(",", db.LastArgs)
});
logger.LogInformation(message);
};
var env = provider.GetRequiredService<IWebHostEnvironment>();
if (env.IsDevelopment())
{
db.CommandExecuted += (sender, args) =>
{
var parameters = new StringBuilder();
foreach (DbParameter p in args.Command.Parameters)
{
parameters.AppendFormat("{0}: {1} ", p.ParameterName, p.Value);
}
logger.LogInformation(args.Command.CommandText);
logger.LogInformation(parameters.ToString());
};
};
return db;
});
// 增加数据服务