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>ͼ
#endregion
builder.Services.AddSingleton<ThumbnailSharpInvoer>();
#region
//ȫ<><C8AB><EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ʼ<EFBFBD><CABC>ֵ
#endregion
GobalModel.SqlLogEnable = Appsettings.appBool("SqlLog_Enable");
//-----------------------------------------------------------------------------------------------------------
var app = builder.Build();
#region

View File

@@ -2,8 +2,8 @@
"profiles": {
"Yi.Framework.ApiMicroservice": {
"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>
"launchBrowser": false,
//<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": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},

View File

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

View File

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

View File

@@ -9,6 +9,8 @@
},
"AllowedHosts": "*",
"SqlLog_Enable": false,
"Consul_Enabled": false,
"DbSeed_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

@@ -28,7 +28,7 @@ namespace Yi.Framework.Common.QueueModel
/// 尝试次数
/// </summary>
public int TryTime { get; set; }
public OrderTypeEnum OrderType { get; set; }
public enum OrderTypeEnum

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 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)
{
JObject jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(this.FileName));
JObject? jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(this.FileName));
if (jObject != null)
{
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();
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)
{
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())
{
roles = new();
}
//先测试部门就是LEBG
long deptId = userRoleMenu.User.DeptId??-1;
long userId = httpContext.GetUserIdInfo();
long deptId = userRoleMenu?.User.DeptId ?? -1;
long userId = httpContext?.GetUserIdInfo()??-1;
//根据角色的数据范围,来添加相对于的数据权限
foreach (var role in roles)
if (roles is not null)
{
//默认为全部
DataScopeEnum dataScope = (DataScopeEnum)(role.DataScope?? DataScopeEnum.ALL.GetHashCode());
switch (dataScope)
foreach (var role in roles)
{
case DataScopeEnum.ALL:
//直接放行
break;
case DataScopeEnum.DEPT:
//只能查询到自己的部门的数据
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.DeptId == deptId, true));
break;
case DataScopeEnum.USER:
//只能查询到自己
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.Id == userId, true));
break;
case DataScopeEnum.CUSTOM:
//自定义查询
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(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);
//默认为全部
DataScopeEnum dataScope = (DataScopeEnum)(role.DataScope ?? DataScopeEnum.ALL.GetHashCode());
switch (dataScope)
{
case DataScopeEnum.ALL:
//直接放行
break;
case DataScopeEnum.DEPT:
//只能查询到自己的部门的数据
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.DeptId == deptId, true));
break;
case DataScopeEnum.USER:
//只能查询到自己
db.QueryFilter.Add(new TableFilterItem<UserEntity>(it => it.Id == userId, true));
break;
case DataScopeEnum.CUSTOM:
//自定义查询
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(filter);
break;
case DataScopeEnum.DEPT_FOLLOW:
//放行自己部门及以下
var allChildDepts = db.Queryable<DeptEntity>().ToChildList(it => it.ParentId, deptId);
db.QueryFilter.Add(filter1);
var filter1 = new TableFilterItem<UserEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.DeptId ?? -1), true);
//部门无需过滤
//var filter2 = new TableFilterItem<DeptEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.Id),true);
//db.QueryFilter.Add(filter2);
break;
default:
break;
db.QueryFilter.Add(filter1);
//部门无需过滤
//var filter2 = new TableFilterItem<DeptEntity>(it => allChildDepts.Select(f => f.Id).ToList().Contains(it.Id),true);
//db.QueryFilter.Add(filter2);
break;
default:
break;
}
}
}
}

View File

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

View File

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

View File

@@ -12,7 +12,7 @@ namespace Yi.Framework.WebCore.FilterExtend
/// </summary>
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>

View File

@@ -14,7 +14,7 @@ namespace Yi.Framework.WebCore.FilterExtend
/// </summary>
public class LogActionFilterAttribute : ActionFilterAttribute
{
private ILogger<LogActionFilterAttribute> _logger = null;
private ILogger<LogActionFilterAttribute> _logger ;
public LogActionFilterAttribute(ILogger<LogActionFilterAttribute> logger)
{
this._logger = logger;
@@ -22,11 +22,11 @@ namespace Yi.Framework.WebCore.FilterExtend
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 controllerName = context.Controller.GetType().FullName;
string actionName = context.ActionDescriptor.DisplayName;
string? controllerName = context.Controller.GetType().FullName;
string? actionName = context.ActionDescriptor.DisplayName;
LogModel logModel = new LogModel()
{

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
@@ -95,10 +96,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
};
db.Aop.OnLogExecuting = (s, p) =>
{
//暂时先关闭sql打印
if (false)
if (GobalModel.SqlLogEnable)
{
var _logger = ServiceLocator.Instance.GetService<ILogger<SqlSugarClient>>();
var _logger = ServiceLocator.Instance?.GetRequiredService<ILogger<SqlSugarClient>>();
StringBuilder sb = new StringBuilder();
sb.Append("执行SQL:" + s.ToString());
@@ -106,8 +106,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
{
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文档注释路径
//获取应用程序所在目录(绝对路径不受工作目录影响建议采用此方法获取路径使用windwos&Linux
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 entityXmlPath = Path.Combine(basePath, @"SwaggerDoc.xml");//实体注释
//c.IncludeXmlComments(apiXmlPath, true);//true表示显示控制器注释

View File

@@ -7,14 +7,14 @@ namespace Yi.Framework.WebCore
{
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 bool GetHttp(out HttpContext httpContext)
public static bool GetHttp(out HttpContext? httpContext)
{
httpContext = null;
var httpContextAccessor = Instance.GetService<IHttpContextAccessor>();
var httpContextAccessor = Instance?.GetService<IHttpContextAccessor>();
if (httpContextAccessor is null)
{
return false;

View File

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