Эх сурвалжийг харах

😁1、调整主机启动时配置获取 2、统一dbconfig字符串类型 3、JSON格式写入文件(对接elk) 4、其他优化调整

zuohuaijun 2 жил өмнө
parent
commit
352e453f43

+ 10 - 10
Admin.NET/Admin.NET.Application/Configuration/Limit.json

@@ -29,29 +29,29 @@
     "HttpStatusCode": 429,
     // API规则,结尾一定要带*
     "GeneralRules": [
-      // 1秒钟只能调用1次
+      // 1秒钟只能调用10
       {
         "Endpoint": "*",
         "Period": "1s",
         "Limit": 10
       },
-      // 1分钟只能调用100次
+      // 1分钟只能调用600次
       {
         "Endpoint": "*",
         "Period": "1m",
-        "Limit": 100
+        "Limit": 600
       },
-      // 1小时只能调用1000
+      // 1小时只能调用3600
       {
         "Endpoint": "*",
         "Period": "1h",
-        "Limit": 1000
+        "Limit": 3600
       },
-      // 1天只能调用10000次
+      // 1天只能调用86400次
       {
         "Endpoint": "*",
         "Period": "1d",
-        "Limit": 10000
+        "Limit": 86400
       }
     ]
   },
@@ -68,7 +68,7 @@
           {
             "Endpoint": "*",
             "Period": "1m",
-            "Limit": 1000
+            "Limit": 600
           }
         ]
       }
@@ -95,7 +95,7 @@
       {
         "Endpoint": "*",
         "Period": "1m",
-        "Limit": 1000
+        "Limit": 600
       }
     ]
   },
@@ -112,7 +112,7 @@
           {
             "Endpoint": "*",
             "Period": "1m",
-            "Limit": 1000
+            "Limit": 600
           }
         ]
       }

+ 2 - 2
Admin.NET/Admin.NET.Application/Configuration/Logging.json

@@ -8,7 +8,7 @@
       "Microsoft.EntityFrameworkCore": "Information"
     },
     "File": {
-      "Enabled": false, // 启用文件日志
+      "Enabled": true, // 启用文件日志
       "FileName": "logs/{0:yyyyMMdd}_{1}.log", // 日志文件
       "Append": true, // 追加覆盖
       // "MinimumLevel": "Information", // 日志级别
@@ -31,7 +31,7 @@
       "BahLogLevel": "Information", // Oops.Oh 和 Oops.Bah 业务日志输出级别
       "WithReturnValue": true, // 是否包含返回值,默认true
       "ReturnValueThreshold": 500, // 返回值字符串阈值,默认0全量输出
-      "JsonBehavior": "None", // 是否输出Json,默认None(OnlyJson、All)
+      "JsonBehavior": "OnlyJson", // 是否输出Json,默认None(OnlyJson、All)
       "JsonIndented": false, // 是否格式化Json
       "UseUtcTimestamp": false // 时间格式UTC、LOCAL
     }

+ 5 - 5
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -22,9 +22,9 @@
     <PackageReference Include="AspNet.Security.OAuth.Weixin" Version="6.0.15" />
     <PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
     <PackageReference Include="FluentEmail.Smtp" Version="3.0.2" />
-    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.4" />
-    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.4" />
-    <PackageReference Include="Furion.Pure" Version="4.9.1.4" />
+    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.6" />
+    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.6" />
+    <PackageReference Include="Furion.Pure" Version="4.9.1.6" />
     <PackageReference Include="IPTools.China" Version="1.6.0" />
     <PackageReference Include="Lazy.Captcha.Core" Version="2.0.6" />
     <PackageReference Include="Magicodes.IE.Excel" Version="2.7.4.5" />
@@ -37,8 +37,8 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.34.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.20.1" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.4.117" />
-    <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.5" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.4.123" />
+    <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.7" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
     <PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="6.0.25" />

+ 1 - 1
Admin.NET/Admin.NET.Core/Cache/CacheSetup.cs

@@ -19,7 +19,7 @@ public static class CacheSetup
     {
         ICache cache = Cache.Default;
 
-        var cacheOptions = App.GetOptions<CacheOptions>();
+        var cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
         if (cacheOptions.CacheType == CacheTypeEnum.Redis.ToString())
         {
             cache = new FullRedis(new RedisOptions

+ 4 - 4
Admin.NET/Admin.NET.Core/Const/CacheConst.cs

@@ -19,10 +19,10 @@ public class CacheConst
     /// </summary>
     public const string KeyUser = "sys_user:";
 
-    /// <summary>
-    /// 用户菜单缓存
-    /// </summary>
-    public const string KeyUserMenu = "sys_user_menu:";
+    ///// <summary>
+    ///// 用户菜单缓存
+    ///// </summary>
+    //public const string KeyUserMenu = "sys_user_menu:";
 
     /// <summary>
     /// 用户权限缓存(按钮集合)

+ 3 - 3
Admin.NET/Admin.NET.Core/Logging/ElasticSearchSetup.cs

@@ -19,11 +19,11 @@ public static class ElasticSearchSetup
 {
     public static void AddElasticSearch(this IServiceCollection services)
     {
-        var enabled = App.GetConfig<bool>("Logging:ElasticSearch:Enabled");
+        var enabled = App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true);
         if (!enabled) return;
 
-        var serverUris = App.GetConfig<List<string>>("Logging:ElasticSearch:ServerUris");
-        var defaultIndex = App.GetConfig<string>("Logging:ElasticSearch:DefaultIndex");
+        var serverUris = App.GetConfig<List<string>>("Logging:ElasticSearch:ServerUris", true);
+        var defaultIndex = App.GetConfig<string>("Logging:ElasticSearch:DefaultIndex", true);
 
         var uris = serverUris.Select(u => new Uri(u));
         var connectionPool = new SniffingConnectionPool(uris);

+ 14 - 3
Admin.NET/Admin.NET.Core/Logging/LoggingSetup.cs

@@ -33,8 +33,9 @@ public static class LoggingSetup
         });
 
         // 日志写入文件
-        if (App.GetConfig<bool>("Logging:File:Enabled"))
+        if (App.GetConfig<bool>("Logging:File:Enabled", true))
         {
+            var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);
             Array.ForEach(new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error }, logLevel =>
             {
                 services.AddFileLogging(options =>
@@ -47,12 +48,22 @@ public static class LoggingSetup
                     {
                         writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
                     };
+                    if (loggingMonitorSettings.JsonBehavior == JsonBehavior.OnlyJson)
+                    {
+                        options.MessageFormat = LoggerFormatter.Json;
+                        // options.MessageFormat = LoggerFormatter.JsonIndented;
+                        options.MessageFormat = (logMsg) =>
+                        {
+                            var jsonString = logMsg.Context.Get("loggingMonitor");
+                            return jsonString?.ToString();
+                        };
+                    }
                 });
             });
         }
 
         // 日志写入数据库
-        if (App.GetConfig<bool>("Logging:Database:Enabled"))
+        if (App.GetConfig<bool>("Logging:Database:Enabled", true))
         {
             services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
             {
@@ -67,7 +78,7 @@ public static class LoggingSetup
         }
 
         // 日志写入ElasticSearch
-        if (App.GetConfig<bool>("Logging:ElasticSearch:Enabled"))
+        if (App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true))
         {
             services.AddDatabaseLogging<ElasticSearchLoggingWriter>(options =>
             {

+ 1 - 1
Admin.NET/Admin.NET.Core/OAuth/OAuthSetup.cs

@@ -21,7 +21,7 @@ public static class OAuthSetup
     /// <param name="services"></param>
     public static void AddOAuth(this IServiceCollection services)
     {
-        var authOpt = App.GetOptions<OAuthOptions>();
+        var authOpt = App.GetConfig<OAuthOptions>("OAuth", true);
         services.AddAuthentication(options =>
             {
                 options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;

+ 1 - 0
Admin.NET/Admin.NET.Core/Option/DbConnectionOptions.cs

@@ -42,6 +42,7 @@ public sealed class DbConnectionConfig : ConnectionConfig
     /// 数据库标识
     /// </summary>
     public new string ConfigId { get; set; }
+
     /// <summary>
     /// 数据库设置
     /// </summary>

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/Menu/SysMenuService.cs

@@ -257,7 +257,7 @@ public class SysMenuService : IDynamicApiController, ITransient
     /// </summary>
     private void DeleteMenuCache()
     {
-        _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserMenu);
+        // _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserMenu);
         _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserButton);
     }
 

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/Role/SysRoleMenuService.cs

@@ -53,7 +53,7 @@ public class SysRoleMenuService : ITransient
         await _sysRoleMenuRep.InsertRangeAsync(menus);
 
         // 清除缓存
-        _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserMenu);
+        // _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserMenu);
         _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserButton);
     }
 

+ 0 - 3
Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs

@@ -17,7 +17,6 @@ public class SysRoleService : IDynamicApiController, ITransient
 {
     private readonly UserManager _userManager;
     private readonly SqlSugarRepository<SysRole> _sysRoleRep;
-    private readonly SysCacheService _sysCacheService;
     private readonly SysRoleOrgService _sysRoleOrgService;
     private readonly SysRoleMenuService _sysRoleMenuService;
     private readonly SysOrgService _sysOrgService;
@@ -25,7 +24,6 @@ public class SysRoleService : IDynamicApiController, ITransient
 
     public SysRoleService(UserManager userManager,
         SqlSugarRepository<SysRole> sysRoleRep,
-        SysCacheService sysCacheService,
         SysRoleOrgService sysRoleOrgService,
         SysRoleMenuService sysRoleMenuService,
         SysOrgService sysOrgService,
@@ -33,7 +31,6 @@ public class SysRoleService : IDynamicApiController, ITransient
     {
         _userManager = userManager;
         _sysRoleRep = sysRoleRep;
-        _sysCacheService = sysCacheService;
         _sysRoleOrgService = sysRoleOrgService;
         _sysRoleMenuService = sysRoleMenuService;
         _sysOrgService = sysOrgService;

+ 3 - 3
Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs

@@ -200,9 +200,9 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("授权用户角色")]
     public async Task GrantRole(UserRoleInput input)
     {
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
-        if (user.AccountType == AccountTypeEnum.SuperAdmin)
-            throw Oops.Oh(ErrorCodeEnum.D1022);
+        //var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
+        //if (user.AccountType == AccountTypeEnum.SuperAdmin)
+        //    throw Oops.Oh(ErrorCodeEnum.D1022);
 
         await _sysUserRoleService.GrantUserRole(input);
     }

+ 3 - 3
Admin.NET/Admin.NET.Core/SignalR/SignalRSetup.cs

@@ -31,12 +31,12 @@ public static class SignalRSetup
         }).AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings));
 
         // 若未启用Redis缓存,直接返回
-        var cacheOptions = App.GetOptions<CacheOptions>();
+        var cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
         if (cacheOptions.CacheType != CacheTypeEnum.Redis.ToString())
             return;
 
         // 若已开启集群配置,则把SignalR配置为支持集群模式
-        var clusterOpt = App.GetOptions<ClusterOptions>();
+        var clusterOpt = App.GetConfig<ClusterOptions>("Cluster", true);
         if (!clusterOpt.Enabled)
             return;
 
@@ -68,7 +68,7 @@ public static class SignalRSetup
             // 原因请参考下边链接:
             // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/RedisHubLifetimeManager.cs#L74
             // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/Internal/RedisChannels.cs#L33
-            options.Configuration.ChannelPrefix = clusterOpt.SignalR.ChannelPrefix;
+            options.Configuration.ChannelPrefix = new RedisChannel(clusterOpt.SignalR.ChannelPrefix, RedisChannel.PatternMode.Auto);
             options.ConnectionFactory = async writer =>
             {
                 ConnectionMultiplexer connection;

+ 1 - 1
Admin.NET/Admin.NET.Core/SignatureAuth/SignatureAuthenticationHandler.cs

@@ -19,7 +19,7 @@ namespace Admin.NET.Core;
 /// </summary>
 public sealed class SignatureAuthenticationHandler : AuthenticationHandler<SignatureAuthenticationOptions>
 {
-    private SysCacheService _cacheService;
+    private readonly SysCacheService _cacheService;
 
     public SignatureAuthenticationHandler(IOptionsMonitor<SignatureAuthenticationOptions> options,
         ILoggerFactory logger,

+ 5 - 4
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -18,19 +18,20 @@ public static class SqlSugarSetup
     public static void AddSqlSugar(this IServiceCollection services)
     {
         // 注册雪花Id
-        YitIdHelper.SetIdGenerator(App.GetOptions<SnowIdOptions>());
+        var snowIdOpt = App.GetConfig<SnowIdOptions>("SnowId", true);
+        YitIdHelper.SetIdGenerator(snowIdOpt);
 
         //// 注册雪花Id-支持分布式
-        //services.AddYitIdHelper(App.GetOptions<SnowIdOptions>());
+        //services.AddYitIdHelper(snowIdOpt);
 
         // 自定义 SqlSugar 雪花ID算法
-        SnowFlakeSingle.WorkId = App.GetOptions<SnowIdOptions>().WorkerId;
+        SnowFlakeSingle.WorkId = snowIdOpt.WorkerId;
         StaticConfig.CustomSnowFlakeFunc = () =>
         {
             return YitIdHelper.NextId();
         };
 
-        var dbOptions = App.GetOptions<DbConnectionOptions>();
+        var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true);
         dbOptions.ConnectionConfigs.ForEach(SetDbConfig);
 
         SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>

+ 2 - 2
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -119,11 +119,11 @@ public class Startup : AppStartup
         });
 
         // OSS对象存储
-        var ossOpt = App.GetOptions<OSSProviderOptions>();
+        var ossOpt = App.GetConfig<OSSProviderOptions>("OSSProvider", true);
         services.AddOSSService(Enum.GetName(ossOpt.Provider), "OSSProvider");
 
         // 电子邮件
-        var emailOpt = App.GetOptions<EmailOptions>();
+        var emailOpt = App.GetConfig<EmailOptions>("Email", true);
         services.AddFluentEmail(emailOpt.DefaultFromEmail, emailOpt.DefaultFromName)
             .AddSmtpSender(new SmtpClient(emailOpt.Host, emailOpt.Port)
             {