Seamus 10 months ago
parent
commit
4b91f556ae

+ 3 - 0
ERP.Application/appsettings.Development.json

@@ -13,5 +13,8 @@
       "/auth/login",
       "/auth/logout"
     ]
+  },
+  "Log": {
+    "LogType": 0
   }
 }

+ 5 - 1
ERP.Core/Application.cs

@@ -10,6 +10,8 @@ using ERP.Framework.Extensions;
 using ERP.Framework.Filter;
 using Newtonsoft.Json;
 using ERP.Framework.Middleware;
+using Serilog;
+using ERP.Framework.Logger;
 
 namespace ERP.Core
 {
@@ -25,7 +27,9 @@ namespace ERP.Core
         {
             // 获取配置项
             var redisConfig = builder.Configuration.GetSection(FrameworkConstant.REDIS).Get<RedisConfig>();
-            //var dbConfig = builder.Configuration.GetSection(FrameworkConstant.DBConfig).Get<DBConfig>();
+            var logConfig = builder.Configuration.GetSection(FrameworkConstant.LOG_CONFIG).Get<LogConfig>();
+
+            Log.Logger = LoggerBuilder.CreateLogger(logConfig);
 
             // 处理Id生成配置
             builder.Configuration.InitIdGenerater();

+ 6 - 0
ERP.Core/Controller/AuthController.cs

@@ -1,5 +1,6 @@
 using ERP.Core.Dto;
 using ERP.Core.Interface;
+using ERP.Framework.Attribute;
 using ERP.Framework.Config;
 using ERP.Framework.Constants;
 using ERP.Framework.Emum;
@@ -23,6 +24,11 @@ namespace ERP.Core.Controller
             _authService = authService;
         }
 
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="form"></param>
+        /// <returns></returns>
         [HttpPost("login")]
         [SwaggerOperation(Description = "登录接口", Summary = "Login")]
         public IActionResult LoginAsync([FromBody] LoginBody form)

+ 2 - 0
ERP.Core/Controller/SysUserController.cs

@@ -2,6 +2,7 @@
 using ERP.Framework.WebApi;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Configuration;
+using Serilog;
 
 namespace ERP.Core.Controller
 {
@@ -20,6 +21,7 @@ namespace ERP.Core.Controller
         [HttpGet]
         public IActionResult Test()
         {
+            Log.Debug("123");
             var m = GetType().Name;
 
             var c = "";

+ 42 - 0
ERP.Framework/Config/LogConfig.cs

@@ -0,0 +1,42 @@
+// <author></author>
+// <date></date>
+// <description></description>
+
+using ERP.Framework.Constants;
+using ERP.Framework.Enum;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ERP.Framework.Config
+{
+    public class LogConfig
+    {
+        /// <summary>
+        /// 日志类别
+        /// </summary>
+        public LogTypeEnum LogType { get; set; }
+
+        /// <summary>
+        /// 文件地址
+        /// </summary>
+        public string FilePath { get; set; } = FrameworkConstant.LOGGING_FILE_PATH;
+
+        /// <summary>
+        /// Els 地址
+        /// </summary>
+        public string? Address { get; set; }
+
+        /// <summary>
+        /// Els 用户名
+        /// </summary>
+        public string? UserName { get; set; }
+
+        /// <summary>
+        /// Els 密码
+        /// </summary>
+        public string? Password { get; set; }
+    }
+}

+ 2 - 1
ERP.Framework/Constants/FrameworkConstant.cs

@@ -13,6 +13,8 @@ namespace ERP.Framework.Constants
         public const string ADMIN_PERMISSION = "*:*:*";
         public const string SECURITY_CONFIG = "Security";
         public const string TOKEN_PREFIX = "Bearer";
+        public const string LOG_CONFIG = "Log";
+        public const string LOGGING_FILE_PATH = "logs/log.log";
 
 
         //public const string AppSetting = "appsettings.json";
@@ -24,7 +26,6 @@ namespace ERP.Framework.Constants
         //public const string ThreadPoolSettings = "ThreadPoolSettings";
         //public const string RabbitMq = "RabbitMq";
         //public const string JWT = "JWT";
-        //public const string Logging = "Logging";
         //public const string Whites = "Whites";
         //public const string Logger = "Logger";
     }

+ 1 - 1
ERP.Framework/ERP.Framework.csproj

@@ -21,8 +21,8 @@
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
-    <PackageReference Include="Serilog" Version="4.0.0" />
     <PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
+    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
     <PackageReference Include="StackExchange.Redis" Version="2.6.122" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
     <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.6.2" />

+ 32 - 0
ERP.Framework/Enum/LogTypeEnum.cs

@@ -0,0 +1,32 @@
+// <author></author>
+// <date></date>
+// <description></description>
+
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ERP.Framework.Enum
+{
+    /// <summary>
+    /// 日志类别
+    /// </summary>
+    [Description("日志类别")]
+    public enum LogTypeEnum
+    {
+        /// <summary>
+        /// 本地
+        /// </summary>
+        [Description("本地")]
+        Local = 0,
+
+        /// <summary>
+        /// Elasticsearch
+        /// </summary>
+        [Description("Elasticsearch")]
+        Els = 1,
+    }
+}

+ 2 - 2
ERP.Framework/Constants/ErrorCodeConstant.cs → ERP.Framework/Exceptions/ErrorCode.cs

@@ -8,9 +8,9 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace ERP.Framework.Constants
+namespace ERP.Framework.Exceptions
 {
-    internal class ErrorCodeConstant
+    internal class ErrorCode
     {
     }
 }

+ 46 - 0
ERP.Framework/Logger/LoggerBuilder.cs

@@ -0,0 +1,46 @@
+// <author></author>
+// <date></date>
+// <description></description>
+
+using ERP.Framework.Config;
+using Serilog;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ERP.Framework.Logger
+{
+    public static class LoggerBuilder
+    {
+        public static ILogger CreateLogger(LogConfig logConfig)
+        {
+            // 扩展增加Elk 或 MongoDB等
+            //if (logConfig.LogType == Enum.LogTypeEnum.Els)
+            //{
+            //   return ... 
+            //}
+
+            return AddLocalLog(logConfig);
+        }
+
+        private static ILogger AddLocalLog(LogConfig logConfig)
+        {
+            var outputTemplete = "[{Timestep:yyyy-MM-dd HH:mm:ss.fff}] {Level} ({SourceContext}) {Message} {NewLine} {Exception}";
+
+            var logConfiguration = new LoggerConfiguration()
+                .MinimumLevel.Debug()
+                .WriteTo.File(logConfig.FilePath,
+                    rollingInterval: RollingInterval.Day,// 文件类别(分支,小时,天)
+                    outputTemplate: outputTemplete, // 日志模板
+                    retainedFileCountLimit:31, // 日志文件最大个数
+                    retainedFileTimeLimit:TimeSpan.FromDays(7), // 日志保存时间
+                    rollOnFileSizeLimit:true, // 是否限制单个文件最大大小
+                    fileSizeLimitBytes:52428800 // 单个文件最大大小
+                    );
+
+            return logConfiguration.CreateLogger();
+        }
+    }
+}

+ 1 - 1
ERP.Framework/Middleware/AuthenticationMiddleware.cs

@@ -31,8 +31,8 @@ namespace ERP.Framework.Middleware
             var path = context.Request.Path.ToString();
             var securityConfig = _configuration.GetSection(FrameworkConstant.SECURITY_CONFIG).Get<SecurityConfig>() ?? new SecurityConfig();
 
-            if (securityConfig.WhiteList != null && securityConfig.WhiteList.Any(w => UrlUtil.Match(w, path)))
             {
+            if (securityConfig.WhiteList != null && securityConfig.WhiteList.Any(w => UrlUtil.Match(w, path)))
                 await next(context);
                 return;
             }