优化代码
This commit is contained in:
@@ -9,12 +9,8 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using SqlSugar.IOC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Admin.WebApi.Framework;
|
||||
@@ -24,13 +20,11 @@ namespace ZR.Admin.WebApi
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
CurrentEnvironment = hostEnvironment;
|
||||
}
|
||||
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private IWebHostEnvironment CurrentEnvironment { get; }
|
||||
public IConfiguration Configuration { get; }
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
@@ -72,7 +66,7 @@ namespace ZR.Admin.WebApi
|
||||
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
||||
});
|
||||
|
||||
InjectServices(services);
|
||||
InjectServices(services, Configuration);
|
||||
|
||||
services.AddMvc(options =>
|
||||
{
|
||||
@@ -94,7 +88,6 @@ namespace ZR.Admin.WebApi
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
//ʹ<><CAB9><EFBFBD>Զ<EFBFBD><D4B6>ζ<EFBFBD>ȥbody<64><79><EFBFBD><EFBFBD>
|
||||
app.Use((context, next) =>
|
||||
@@ -135,59 +128,15 @@ namespace ZR.Admin.WebApi
|
||||
/// ע<><D7A2>Services<65><73><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
private void InjectServices(IServiceCollection services)
|
||||
/// <param name="configuration"></param>
|
||||
private void InjectServices(IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddAppService();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
services.AddTaskSchedulers();
|
||||
|
||||
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
||||
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
||||
string dbKey = Configuration[OptionsSetting.DbKey];
|
||||
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
||||
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
|
||||
|
||||
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
|
||||
new IocConfig() {
|
||||
ConfigId = "0",
|
||||
ConnectionString = connStr,
|
||||
DbType = (IocDbType)dbType,
|
||||
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
||||
}, new IocConfig() {
|
||||
ConfigId = "1",
|
||||
ConnectionString = connStrBus,
|
||||
DbType = (IocDbType)dbType_bus,
|
||||
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
||||
}
|
||||
});
|
||||
|
||||
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡSQL
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//Console.WriteLine("<22><>SQL<51><4C><EFBFBD>䡿" + sql.ToLower() + "\r\n" + param);
|
||||
logger.Info($"Sql<71><6C><EFBFBD>䣺{sql}<7D><>{param}");
|
||||
};
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>־
|
||||
DbScoped.SugarScope.GetConnection(0).Aop.OnError = (e) =>
|
||||
{
|
||||
Console.WriteLine($"[ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>]{e.Message}<7D><>SQL={e.Sql}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡSQL
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
|
||||
//Console.WriteLine("<22><>SQL<51><4C><EFBFBD><EFBFBD>Bus<75><73>" + sql.ToLower() + "\r\n" + param);
|
||||
logger.Info($"Sql<71><6C><EFBFBD>䣺{sql}, {param}");
|
||||
};
|
||||
//Bus Db<44><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
||||
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
|
||||
{
|
||||
logger.Error($"ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>{e.Sql}<7D><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>{e.Message}");
|
||||
};
|
||||
//<2F><>ʼ<EFBFBD><CABC>db
|
||||
DbExtension.AddDb(configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user