sql日志打印输出配置

This commit is contained in:
陈淳
2022-10-18 09:01:16 +08:00
parent 9d365dbf1e
commit dab4a092d9
20 changed files with 100 additions and 66 deletions

View File

@@ -142,6 +142,11 @@ builder.Services.AddHttpContextAccessor();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
#endregion #endregion
builder.Services.AddSingleton<ThumbnailSharpInvoer>(); builder.Services.AddSingleton<ThumbnailSharpInvoer>();
#region
//ȫ<><C8AB><EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ʼ<EFBFBD><CABC>ֵ
#endregion
GobalModel.SqlLogEnable = Appsettings.appBool("SqlLog_Enable");
//----------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------
var app = builder.Build(); var app = builder.Build();
#region #region

View File

@@ -2,8 +2,8 @@
"profiles": { "profiles": {
"Yi.Framework.ApiMicroservice": { "Yi.Framework.ApiMicroservice": {
"commandName": "Project", "commandName": "Project",
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸о<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>عرȽ<EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
"launchBrowser": false, "launchBrowser": true,
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },

View File

@@ -9,6 +9,8 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"SqlLog_Enable": false,
"Consul_Enabled": false, "Consul_Enabled": false,
"Apollo_Enabled": false, "Apollo_Enabled": false,
"HealthCheck_Enabled": false, "HealthCheck_Enabled": false,

View File

@@ -9,6 +9,8 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"SqlLog_Enable": false,
"Consul_Enabled": false, "Consul_Enabled": false,
"DbSeed_Enabled": false, "DbSeed_Enabled": false,
"Apollo_Enabled": false, "Apollo_Enabled": false,

View File

@@ -9,6 +9,8 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"SqlLog_Enable": false,
"Consul_Enabled": false, "Consul_Enabled": false,
"DbSeed_Enabled": false, "DbSeed_Enabled": false,
"Apollo_Enabled": false, "Apollo_Enabled": false,

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Common.Models
{
public static class GobalModel
{
public static bool SqlLogEnable { get; set; } = true;
}
}

View File

@@ -84,9 +84,9 @@ namespace Yi.Framework.WebCore
} }
public static IConfiguration appConfiguration(params string[] sections) public static IConfiguration? appConfiguration(params string[] sections)
{ {
return Configuration.GetSection(string.Join(":", sections)); return Configuration?.GetSection(string.Join(":", sections));
} }
} }
} }

View File

@@ -52,10 +52,14 @@ namespace Yi.Framework.WebCore.BuilderExtend
{ {
var property = apolloProvider.GetType().BaseType?.GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic); var property = apolloProvider.GetType().BaseType?.GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic);
var data = property?.GetValue(apolloProvider) as IDictionary<string, string>; var data = property?.GetValue(apolloProvider) as IDictionary<string, string>;
foreach (var item in data) if (data is not null)
{ {
Console.WriteLine($"key {item.Key} value {item.Value}"); foreach (var item in data)
{
Console.WriteLine($"key {item.Key} value {item.Value}");
}
} }
} }
}); });
} }

View File

@@ -15,12 +15,12 @@ internal class JsonOptionsWritable<TOptions> : FileOptionsWritableBase<TOptions>
public override void Update(Action<TOptions> configuration) public override void Update(Action<TOptions> configuration)
{ {
JObject jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(this.FileName)); JObject? jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(this.FileName));
if (jObject != null) if (jObject != null)
{ {
TOptions option = this.Monitor.CurrentValue ?? new TOptions(); TOptions option = this.Monitor.CurrentValue ?? new TOptions();
if (jObject.TryGetValue(this.Section, out JToken jtoken)) if (jObject.TryGetValue(this.Section, out JToken? jtoken))
{ {
option = JsonConvert.DeserializeObject<TOptions>(jtoken.ToString()) ?? new TOptions(); option = JsonConvert.DeserializeObject<TOptions>(jtoken.ToString()) ?? new TOptions();
configuration?.Invoke(option); configuration?.Invoke(option);

View File

@@ -21,7 +21,7 @@ namespace Yi.Framework.Core
} }
//无需授权情况 //无需授权情况
var userName = httpContext.GetUserNameInfo(); var userName = httpContext?.GetUserNameInfo();
if (userName is null) if (userName is null)
{ {
return; return;
@@ -35,54 +35,57 @@ namespace Yi.Framework.Core
//这里可以优化一下 //这里可以优化一下
//根据缓存获取全部用户信息 //根据缓存获取全部用户信息
var userRoleMenu = ServiceLocator.Instance.GetService<CacheInvoker>().Get<UserRoleMenuDto>("用户id"); var userRoleMenu = ServiceLocator.Instance?.GetService<CacheInvoker>()?.Get<UserRoleMenuDto>("用户id");
var roles = userRoleMenu.Roles; var roles = userRoleMenu?.Roles;
if (roles.IsNull()) if (roles.IsNull())
{ {
roles = new(); roles = new();
} }
//先测试部门就是LEBG //先测试部门就是LEBG
long deptId = userRoleMenu.User.DeptId??-1; long deptId = userRoleMenu?.User.DeptId ?? -1;
long userId = httpContext.GetUserIdInfo(); long userId = httpContext?.GetUserIdInfo()??-1;
//根据角色的数据范围,来添加相对于的数据权限 //根据角色的数据范围,来添加相对于的数据权限
foreach (var role in roles) if (roles is not null)
{ {
//默认为全部 foreach (var role in roles)
DataScopeEnum dataScope = (DataScopeEnum)(role.DataScope?? DataScopeEnum.ALL.GetHashCode());
switch (dataScope)
{ {
case DataScopeEnum.ALL: //默认为全部
//直接放行 DataScopeEnum dataScope = (DataScopeEnum)(role.DataScope ?? DataScopeEnum.ALL.GetHashCode());
break; switch (dataScope)
case DataScopeEnum.DEPT: {
//只能查询到自己的部门的数据 case DataScopeEnum.ALL:
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.DeptId == deptId, true)); //直接放行
break; break;
case DataScopeEnum.USER: case DataScopeEnum.DEPT:
//只能查询到自己 //只能查询到自己的部门的数据
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.Id == userId, true)); db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.DeptId == deptId, true));
break; break;
case DataScopeEnum.CUSTOM: case DataScopeEnum.USER:
//自定义查询 //只能查询到自己
var filter = new TableFilterItem<UserEntity>(it => SqlFunc.Subqueryable<RoleDeptEntity>().Where(f => f.DeptId == it.DeptId && f.RoleId == (long)role.Id).Any(), true); db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.Id == userId, true));
db.QueryFilter.Add(filter); break;
break; case DataScopeEnum.CUSTOM:
case DataScopeEnum.DEPT_FOLLOW: //自定义查询
//放行自己部门及以下 var filter = new TableFilterItem<UserEntity>(it => SqlFunc.Subqueryable<RoleDeptEntity>().Where(f => f.DeptId == it.DeptId && f.RoleId == (long)role.Id).Any(), true);
var allChildDepts = db.Queryable<DeptEntity>().ToChildList(it => it.ParentId, deptId); db.QueryFilter.Add(filter);
break;
case DataScopeEnum.DEPT_FOLLOW:
//放行自己部门及以下
var allChildDepts = db.Queryable<DeptEntity>().ToChildList(it => it.ParentId, deptId);
var filter1 = new TableFilterItem<UserEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.DeptId??-1), true); var filter1 = new TableFilterItem<UserEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.DeptId ?? -1), true);
db.QueryFilter.Add(filter1); db.QueryFilter.Add(filter1);
//部门无需过滤 //部门无需过滤
//var filter2 = new TableFilterItem<DeptEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.Id),true); //var filter2 = new TableFilterItem<DeptEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.Id),true);
//db.QueryFilter.Add(filter2); //db.QueryFilter.Add(filter2);
break; break;
default: default:
break; break;
}
} }
} }
} }

View File

@@ -93,7 +93,7 @@ namespace Yi.Framework.WebCore.DbExtend
if (Appsettings.appBool("DbSeed_Enabled")) if (Appsettings.appBool("DbSeed_Enabled"))
{ {
var _Db = app.ApplicationServices.GetService<ISqlSugarClient>(); var _Db = app.ApplicationServices.GetRequiredService<ISqlSugarClient>();
Invoer(_Db); Invoer(_Db);
} }

View File

@@ -27,8 +27,8 @@ namespace Yi.Framework.WebCore.FilterExtend
{ {
context.Result = new JsonResult( context.Result = new JsonResult(
Result.Error("操作失败").SetData(context.Exception.Message)); Result.Error("操作失败").SetData(context.Exception.Message));
string url = context.HttpContext.Request.Path.Value; string? url = context.HttpContext.Request.Path.Value;
string actionName = context.ActionDescriptor.DisplayName; string? actionName = context.ActionDescriptor.DisplayName;
var logModel = new LogModel() var logModel = new LogModel()
{ {

View File

@@ -12,7 +12,7 @@ namespace Yi.Framework.WebCore.FilterExtend
/// </summary> /// </summary>
public class CustomResourceFilterAttribute : Attribute, IResourceFilter, IFilterMetadata public class CustomResourceFilterAttribute : Attribute, IResourceFilter, IFilterMetadata
{ {
private static Dictionary<string, IActionResult> CustomCache = new Dictionary<string, IActionResult>(); private static Dictionary<string, IActionResult?> CustomCache = new Dictionary<string, IActionResult?>();
/// <summary> /// <summary>
/// 发生在其他动作之前 /// 发生在其他动作之前
/// </summary> /// </summary>

View File

@@ -14,7 +14,7 @@ namespace Yi.Framework.WebCore.FilterExtend
/// </summary> /// </summary>
public class LogActionFilterAttribute : ActionFilterAttribute public class LogActionFilterAttribute : ActionFilterAttribute
{ {
private ILogger<LogActionFilterAttribute> _logger = null; private ILogger<LogActionFilterAttribute> _logger ;
public LogActionFilterAttribute(ILogger<LogActionFilterAttribute> logger) public LogActionFilterAttribute(ILogger<LogActionFilterAttribute> logger)
{ {
this._logger = logger; this._logger = logger;
@@ -22,11 +22,11 @@ namespace Yi.Framework.WebCore.FilterExtend
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
string url = context.HttpContext.Request.Path.Value; string? url = context.HttpContext.Request.Path.Value;
string argument = JsonConvert.SerializeObject(context.ActionArguments); string argument = JsonConvert.SerializeObject(context.ActionArguments);
string controllerName = context.Controller.GetType().FullName; string? controllerName = context.Controller.GetType().FullName;
string actionName = context.ActionDescriptor.DisplayName; string? actionName = context.ActionDescriptor.DisplayName;
LogModel logModel = new LogModel() LogModel logModel = new LogModel()
{ {

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Common.Models;
namespace Yi.Framework.WebCore.MiddlewareExtend namespace Yi.Framework.WebCore.MiddlewareExtend
{ {
@@ -95,10 +96,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
}; };
db.Aop.OnLogExecuting = (s, p) => db.Aop.OnLogExecuting = (s, p) =>
{ {
//暂时先关闭sql打印 if (GobalModel.SqlLogEnable)
if (false)
{ {
var _logger = ServiceLocator.Instance.GetService<ILogger<SqlSugarClient>>(); var _logger = ServiceLocator.Instance?.GetRequiredService<ILogger<SqlSugarClient>>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append("执行SQL:" + s.ToString()); sb.Append("执行SQL:" + s.ToString());
@@ -106,8 +106,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
{ {
sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}"); sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}");
} }
_logger?.LogInformation(sb.ToString());
_logger.LogInformation(sb.ToString());
} }

View File

@@ -31,6 +31,10 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
//为 Swagger JSON and UI设置xml文档注释路径 //为 Swagger JSON and UI设置xml文档注释路径
//获取应用程序所在目录(绝对路径不受工作目录影响建议采用此方法获取路径使用windwos&Linux //获取应用程序所在目录(绝对路径不受工作目录影响建议采用此方法获取路径使用windwos&Linux
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
if (basePath is null)
{
throw new Exception("未找到swagger文件");
}
var apiXmlPath = Path.Combine(basePath, @"Config/SwaggerDoc.xml");//控制器层注释 var apiXmlPath = Path.Combine(basePath, @"Config/SwaggerDoc.xml");//控制器层注释
//var entityXmlPath = Path.Combine(basePath, @"SwaggerDoc.xml");//实体注释 //var entityXmlPath = Path.Combine(basePath, @"SwaggerDoc.xml");//实体注释
//c.IncludeXmlComments(apiXmlPath, true);//true表示显示控制器注释 //c.IncludeXmlComments(apiXmlPath, true);//true表示显示控制器注释

View File

@@ -7,14 +7,14 @@ namespace Yi.Framework.WebCore
{ {
public static class ServiceLocator public static class ServiceLocator
{ {
public static IServiceProvider Instance { get; set; } public static IServiceProvider? Instance { get; set; }
public static string Admin { get; set; } = "cc"; public static string Admin { get; set; } = "cc";
public static bool GetHttp(out HttpContext httpContext) public static bool GetHttp(out HttpContext? httpContext)
{ {
httpContext = null; httpContext = null;
var httpContextAccessor = Instance.GetService<IHttpContextAccessor>(); var httpContextAccessor = Instance?.GetService<IHttpContextAccessor>();
if (httpContextAccessor is null) if (httpContextAccessor is null)
{ {
return false; return false;

View File

@@ -40,11 +40,11 @@ namespace Yi.Framework.WebCore.SignalRHub
{ {
OnlineUser users = new(Context.ConnectionId) OnlineUser users = new(Context.ConnectionId)
{ {
Browser= loginUser.Browser, Browser= loginUser?.Browser,
LoginLocation = loginUser.LoginLocation, LoginLocation = loginUser?.LoginLocation,
Ipaddr= loginUser.LoginIp, Ipaddr= loginUser?.LoginIp,
LoginTime=DateTime.Now, LoginTime=DateTime.Now,
Os=loginUser.Os, Os=loginUser?.Os,
UserName= name??"" UserName= name??""
}; };
clientUsers.Add(users); clientUsers.Add(users);