diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs index 7b09bd26..16659b3e 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Extensions/ServicesExtensions.cs @@ -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(); var connString = configuration.GetConnectionString("bb"); - return new Database(connString, new BootstrapAdminConventionMapper()); + var db = new Database(connString, new BootstrapAdminConventionMapper()); + + var logger = provider.GetRequiredService>(); + 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(); + 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; }); // 增加数据服务