diff --git a/projects/Infrastructure/Email/EmailSender.cs b/projects/Infrastructure/Email/EmailSender.cs index 4f6528d7..7fc9c713 100644 --- a/projects/Infrastructure/Email/EmailSender.cs +++ b/projects/Infrastructure/Email/EmailSender.cs @@ -5,7 +5,6 @@ namespace Infrastructure.Email { public class EmailSender : IEmailSender { - [System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5359:请勿禁用证书验证", Justification = "<挂起>")] public void SendMail(string name, string from, string to, diff --git a/projects/Infrastructure/Extensions/ExpressionExtensions.cs b/projects/Infrastructure/Extensions/ExpressionExtensions.cs index 4ece8ae7..b6f9a742 100644 --- a/projects/Infrastructure/Extensions/ExpressionExtensions.cs +++ b/projects/Infrastructure/Extensions/ExpressionExtensions.cs @@ -18,8 +18,7 @@ namespace Infrastructure.Extensions public static string MemberName(this Expression> expression) { - var memberExpression = expression.Body as MemberExpression; - if (memberExpression == null) + if (expression.Body is not MemberExpression memberExpression) throw new InvalidOperationException("Expression must be a member expression"); return memberExpression.Member.Name; diff --git a/projects/Infrastructure/Infrastructure.csproj b/projects/Infrastructure/Infrastructure.csproj index 030a1665..09397338 100644 --- a/projects/Infrastructure/Infrastructure.csproj +++ b/projects/Infrastructure/Infrastructure.csproj @@ -10,35 +10,35 @@ - - - - - + + + + + - - + + - - - - + + + + - + - + - + - - - + + + diff --git a/projects/Infrastructure/Sms/NetEasySmsSender.cs b/projects/Infrastructure/Sms/NetEasySmsSender.cs index 38e8df37..a8393b4e 100644 --- a/projects/Infrastructure/Sms/NetEasySmsSender.cs +++ b/projects/Infrastructure/Sms/NetEasySmsSender.cs @@ -38,9 +38,7 @@ namespace Infrastructure.Sms { try { -#pragma warning disable CA2000 // 丢失范围之前释放对象 var client = this._httpClientFactory.CreateClient(); -#pragma warning restore CA2000 // 丢失范围之前释放对象 var formData = new List> { new KeyValuePair("AppKey", this._appKey), @@ -50,9 +48,7 @@ namespace Infrastructure.Sms formData.Add(new KeyValuePair("CurTime", seconds)); var sumValue = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", this._appSecret, formData[1].Value, formData[2].Value); var sumBytes = Encoding.UTF8.GetBytes(sumValue); -#pragma warning disable CA5350 // 不要使用弱加密算法 using var sha1 = SHA1.Create(); -#pragma warning restore CA5350 // 不要使用弱加密算法 var sumHash = sha1.ComputeHash(sumBytes); var hashValue = string.Empty; var builder = new StringBuilder(); diff --git a/projects/Infrastructure/Web/BaseStartup.cs b/projects/Infrastructure/Web/BaseStartup.cs index f584327e..b539bd5e 100644 --- a/projects/Infrastructure/Web/BaseStartup.cs +++ b/projects/Infrastructure/Web/BaseStartup.cs @@ -279,7 +279,6 @@ namespace Infrastructure.Web { if (!context.Request.IsAjax()) { - var basePath = this.Configuration.GetAppSetting("BasePath"); var linkGenerator = context.HttpContext.RequestServices.GetRequiredService(); var url = linkGenerator.GetPathByAction("Login","Account",pathBase:context.HttpContext.Request.PathBase); context.Response.Redirect(url); diff --git a/projects/IoT.Shared/IoT.Shared.csproj b/projects/IoT.Shared/IoT.Shared.csproj index cf717f00..189fc6c1 100644 --- a/projects/IoT.Shared/IoT.Shared.csproj +++ b/projects/IoT.Shared/IoT.Shared.csproj @@ -7,11 +7,11 @@ 1.1.0.0 - - - + + + - + diff --git a/projects/IoTDameon/IoTDameon.csproj b/projects/IoTDameon/IoTDameon.csproj index 0ae453c9..0a68820a 100644 --- a/projects/IoTDameon/IoTDameon.csproj +++ b/projects/IoTDameon/IoTDameon.csproj @@ -7,7 +7,7 @@ - + diff --git a/projects/IoTNode/IoTNode.csproj b/projects/IoTNode/IoTNode.csproj index 6944b7b3..38ca97d6 100644 --- a/projects/IoTNode/IoTNode.csproj +++ b/projects/IoTNode/IoTNode.csproj @@ -13,7 +13,7 @@ - + diff --git a/projects/Platform/Controllers/LogController.cs b/projects/Platform/Controllers/LogController.cs new file mode 100644 index 00000000..4f554c65 --- /dev/null +++ b/projects/Platform/Controllers/LogController.cs @@ -0,0 +1,114 @@ +using Infrastructure.Application; +using Infrastructure.Extensions; +using Infrastructure.Web; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Serilog.Events; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; + +namespace Platform.Controllers +{ + public class LogController : BaseController + { + public IActionResult Index(PagedListModel model) + { + using var db = new LogDbContext(); + + var query = db.Set().AsNoTracking() + .WhereIf(!string.IsNullOrEmpty(model.Query.Level), o => o.Level == model.Query.Level) + .WhereIf(!string.IsNullOrEmpty(model.Query.RenderedMessage), o => o.RenderedMessage.Contains(model.Query.RenderedMessage.Trim())) + .WhereIf(!string.IsNullOrEmpty(model.Query.Exception), o => o.Exception.Contains(model.Query.Exception.Trim())) + .WhereIf(!string.IsNullOrEmpty(model.Query.Properties), o => o.Properties.Contains(model.Query.Properties.Trim())); + + model.TotalCount = query.Count(); + var list = query + .Skip((model.PageIndex - 1) * model.PageSize) + .Take(model.PageSize) + .ToList(); + model.List.AddRange(list); + var levels = new List { + nameof(LogEventLevel.Verbose), + nameof(LogEventLevel.Debug), + nameof(LogEventLevel.Information), + nameof(LogEventLevel.Warning), + nameof(LogEventLevel.Error), + nameof(LogEventLevel.Fatal) + }; + ViewData.SelectList(o => model.Query.Level, () => new SelectList(levels, model.Query.Level)); + return View(model); + } + + [HttpPost] + public virtual IActionResult Delete(List list) + { + if (list == null) + { + throw new ArgumentNullException(nameof(list)); + } + try + { + using var db = new LogDbContext(); + var query = db.Set(); + foreach (var id in list) + { + var entity = query.FirstOrDefault(o => o.Id == id); + query.Remove(entity); + } + db.SaveChanges(); + return RedirectTo(); + } + catch (Exception ex) + { + ex.PrintStack(); + return RedirectTo(rawMesage:ex.Message); + } + } + } + + public class Logs + { + [Display(Name = "主键")] + public int Id { get; set; } + + [Display(Name ="时间")] + [DataType(DataType.DateTime)] + public DateTime? Timestamp { get; set; } + + [Display(Name = "等级")] + [SelectList] + public string Level { get; set; } + [Display(Name = "异常")] + public string Exception { get; set; } + [Display(Name = "消息")] + public string RenderedMessage { get; set; } + [Display(Name = "属性")] + public string Properties { get; set; } + } + + internal class LogDbContext : DbContext + { + public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => + { + builder.AddDebug(); + builder.AddConsole(); + }); + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=log.db"); + optionsBuilder?.UseLoggerFactory(MyLoggerFactory); + optionsBuilder?.EnableSensitiveDataLogging(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity().HasKey(o => o.Id); + } + } +} \ No newline at end of file diff --git a/projects/Platform/Platform.csproj b/projects/Platform/Platform.csproj index 84660cd9..fa90ee9c 100644 --- a/projects/Platform/Platform.csproj +++ b/projects/Platform/Platform.csproj @@ -10,6 +10,7 @@ + diff --git a/projects/Platform/Program.cs b/projects/Platform/Program.cs index d65cbbbb..32c36f4e 100644 --- a/projects/Platform/Program.cs +++ b/projects/Platform/Program.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Serilog; +using Serilog.Settings.Configuration; using System; using System.IO; using System.Text; @@ -15,20 +16,41 @@ namespace Platform public static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; + var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true) .Build(); - WebHost.CreateDefaultBuilder(args) + + var logConfig = new LoggerConfiguration() + .ReadFrom.Configuration(config, ConfigurationAssemblySource.AlwaysScanDllFiles) + .WriteTo.Console(); + + logConfig = config.GetSection("AppSetting").GetValue("Log", "File") == "File" + ? logConfig.WriteTo.File("logs/log.txt", rollOnFileSizeLimit: true, fileSizeLimitBytes: 100 * 1024 * 1024, rollingInterval: RollingInterval.Infinite) + : logConfig.WriteTo.SQLite(Path.Combine(Directory.GetCurrentDirectory(), "log.db")); + + Log.Logger = logConfig.CreateLogger(); + + try + { + Log.Information("web host start"); + WebHost.CreateDefaultBuilder(args) .UseConfiguration(config) - .ConfigureLogging((c, o) => - { - Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); - o.AddSerilog(); - Log.Logger.Information(string.IsNullOrEmpty(config["docker"]) ? "start..." : "docker..."); - }) + .UseSerilog() .UseStartup() .Build() .Run(); + Log.Information("web host end"); + } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly"); + return; + } + finally + { + Log.CloseAndFlush(); + } } } } \ No newline at end of file diff --git a/projects/Platform/Views/Log/Index.cshtml b/projects/Platform/Views/Log/Index.cshtml new file mode 100644 index 00000000..3d22b835 --- /dev/null +++ b/projects/Platform/Views/Log/Index.cshtml @@ -0,0 +1,91 @@ +@model PagedListModel +@{ + var start = 0; +} +
+
+
+
+
+
+ +
+ @Html.EditorFor(o => o.Query.Level) +
+
+
+
+
+ +
+ @Html.EditorFor(o => o.Query.RenderedMessage) +
+
+
+
+
+ +
+ @Html.EditorFor(o => o.Query.Exception) +
+
+
+
+
+ +
+ @Html.EditorFor(o => o.Query.Properties) +
+
+
+
+
+
+
+
+
+ + +
+
+
+
+
+
+ + + + + + + + + + + + @foreach (var item in Model.List) + { + + + + + + + + + + } +
行号IdTimestampLevelExceptionRenderedMessageProperties
+ + @(++start)@item.Id@item.Timestamp@item.Level@item.Exception + @item.RenderedMessage@item.Properties
+
+ +
+
\ No newline at end of file diff --git a/projects/Platform/appsettings.json b/projects/Platform/appsettings.json index e50fdfc0..e4b20b08 100644 --- a/projects/Platform/appsettings.json +++ b/projects/Platform/appsettings.json @@ -14,9 +14,10 @@ "BasePath": "/platform", "TablePrefix": "", "Database": "MySQL", + "JobDatabase": "MySQL", + "Log": "SQLite", "FileServer": "MinIO", "Cache": "Redis", - "JobDatabase": "MySQL", "sms": "url=https://api.netease.im/sms/sendcode.action;key=54ebdf1f4364ddd8ff6f82f88d630a76;secret=c49d42b67798@" }, "ConnectionStrings": { diff --git a/projects/WebMVC/WebMVC.csproj b/projects/WebMVC/WebMVC.csproj index 966f2957..92546b1c 100644 --- a/projects/WebMVC/WebMVC.csproj +++ b/projects/WebMVC/WebMVC.csproj @@ -6,6 +6,6 @@ - + \ No newline at end of file diff --git a/projects/WebSPA/WebSPA.csproj b/projects/WebSPA/WebSPA.csproj index 4bb429b0..dcf5f298 100644 --- a/projects/WebSPA/WebSPA.csproj +++ b/projects/WebSPA/WebSPA.csproj @@ -6,6 +6,6 @@ - + \ No newline at end of file