feat: 增加 SQL 语句执行输出
This commit is contained in:
parent
5eddf6ad24
commit
fe36e7d888
|
@ -3,10 +3,16 @@ using BootstrapAdmin.DataAccess.PetaPoco.Services;
|
||||||
using BootstrapAdmin.Web.Core;
|
using BootstrapAdmin.Web.Core;
|
||||||
using BootstrapBlazor.Components;
|
using BootstrapBlazor.Components;
|
||||||
using BootstrapBlazor.DataAcces.PetaPoco.Services;
|
using BootstrapBlazor.DataAcces.PetaPoco.Services;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using PetaPoco;
|
using PetaPoco;
|
||||||
using PetaPoco.Providers;
|
using PetaPoco.Providers;
|
||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.DependencyInjection
|
namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
|
@ -27,7 +33,33 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
//TODO: 后期改造成自定适配
|
//TODO: 后期改造成自定适配
|
||||||
var configuration = provider.GetRequiredService<IConfiguration>();
|
var configuration = provider.GetRequiredService<IConfiguration>();
|
||||||
var connString = configuration.GetConnectionString("bb");
|
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;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 增加数据服务
|
// 增加数据服务
|
||||||
|
|
Loading…
Reference in New Issue