From 9b3c1cd057dad4b8d0615a2b2961cb76f09d239b Mon Sep 17 00:00:00 2001 From: wenjy Date: Sat, 12 Oct 2024 14:42:19 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E7=B3=BB=E7=BB=9F=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E6=A0=B7=E5=BC=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Ioc/DependencyConfigurator.cs | 4 ++ SlnMesnac.Serilog/SerilogExtensions.cs | 23 ++++-- SlnMesnac.Serilog/SerilogHelper.cs | 95 +++++++++++++++++++++++++ SlnMesnac.WPF/App.xaml.cs | 11 +-- SlnMesnac.WPF/MainWindow.xaml | 54 ++++---------- SlnMesnac.WPF/appsettings.json | 2 +- 6 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 SlnMesnac.Serilog/SerilogHelper.cs diff --git a/SlnMesnac.Ioc/DependencyConfigurator.cs b/SlnMesnac.Ioc/DependencyConfigurator.cs index c7d2db9..197f20a 100644 --- a/SlnMesnac.Ioc/DependencyConfigurator.cs +++ b/SlnMesnac.Ioc/DependencyConfigurator.cs @@ -1,5 +1,6 @@ using Autofac; using SlnMesnac.Repository; +using SlnMesnac.Serilog; using System.Reflection; using TouchSocket.Sockets; @@ -60,6 +61,9 @@ namespace SlnMesnac.Ioc //注入代码生成 RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Generate.dll")); + + //注入Serilog日志帮助类 + builder.RegisterType(typeof(SerilogHelper)).SingleInstance(); } diff --git a/SlnMesnac.Serilog/SerilogExtensions.cs b/SlnMesnac.Serilog/SerilogExtensions.cs index 6f9b303..f38f112 100644 --- a/SlnMesnac.Serilog/SerilogExtensions.cs +++ b/SlnMesnac.Serilog/SerilogExtensions.cs @@ -45,12 +45,25 @@ namespace SlnMesnac.Serilog var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; #endregion + //Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console() + // .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information) + // .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error) + // .WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning) + // //.WriteTo.File(Path.Combine(logPath, "Debug.log"), LogEventLevel.Debug, fileSizeLimitBytes: 5 * 1024) + // .CreateLogger(); + Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console() - .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information) - .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error) - .WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning) - //.WriteTo.File(Path.Combine(logPath, "Debug.log"), LogEventLevel.Debug, fileSizeLimitBytes: 5 * 1024) - .CreateLogger(); + .WriteTo.Logger(lc => lc + .Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Info")) + .WriteTo.File(Path.Combine(logPath, "Info.log"))) + .WriteTo.Logger(lc => lc + .Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Plc")) + .WriteTo.File(Path.Combine(logPath, "Plc.log"))) + .WriteTo.Logger(lc => lc + .Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Error")) + .WriteTo.File(Path.Combine(logPath, "Error.log"))) + .CreateLogger(); + app.UseSerilogRequestLogging(); return app; diff --git a/SlnMesnac.Serilog/SerilogHelper.cs b/SlnMesnac.Serilog/SerilogHelper.cs new file mode 100644 index 0000000..6e432c8 --- /dev/null +++ b/SlnMesnac.Serilog/SerilogHelper.cs @@ -0,0 +1,95 @@ +using Serilog; +using System; +using System.Collections.Generic; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:SlnMesnac.Serilog +* 唯一标识:a537790e-bf7c-4dd6-ade7-fc8cd5bd6d0d +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2024-10-12 10:42:44 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Serilog +{ + /// + /// Serilog日志类 + /// + public class SerilogHelper + { + private readonly ILogger? Info_logger = Log.ForContext("Module", "Info"); + private readonly ILogger? Plc_logger = Log.ForContext("Module", "Plc"); + private readonly ILogger? Error_logger = Log.ForContext("Module", "Error"); + + /// + /// Info日志 + /// + /// + public void Info(string msg) + { + if(Info_logger!= null) + { + this.Info_logger.Information(msg); + } + } + + /// + /// Plc日志 + /// + /// + public void Plc(string msg) + { + if (Plc_logger != null) + { + this.Plc_logger.Information(msg); + } + } + + /// + /// Error日志 + /// + /// + /// + public void Error(string msg, Exception ex = null) + { + if (!string.IsNullOrEmpty(msg) && ex == null) + { + this.Error_logger.Information("【附加信息】 : {0}
", new object[] { msg }); + } + else if (!string.IsNullOrEmpty(msg) && ex != null) + { + string errorMsg = BeautyErrorMsg(ex); + this.Error_logger.Information("【附加信息】 : {0}
{1}", new object[] { msg, errorMsg }); + } + else if (string.IsNullOrEmpty(msg) && ex != null) + { + string errorMsg = BeautyErrorMsg(ex); + this.Error_logger.Information(errorMsg); + } + } + + private string BeautyErrorMsg(Exception ex) + { + string errorMsg = string.Format("【异常类型】:{0}
【异常信息】:{1}
【堆栈调用】:{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace }); + errorMsg = errorMsg.Replace("\r\n", "
"); + errorMsg = errorMsg.Replace("位置", "位置"); + return errorMsg; + } + + } +} diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index 429d2bb..c5aa948 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -7,6 +7,7 @@ using SlnMesnac.Config; using System; using System.Windows; using Autofac.Extensions.DependencyInjection; +using SlnMesnac.Serilog; namespace SlnMesnac.WPF { @@ -18,6 +19,7 @@ namespace SlnMesnac.WPF private System.Threading.Mutex? mutex = null; private LierdaCracker cracker = new LierdaCracker(); public static IServiceProvider? ServiceProvider = null; + private SerilogHelper serilogHelper = null; // Startup事件 protected override async void OnStartup(StartupEventArgs e) @@ -36,13 +38,12 @@ namespace SlnMesnac.WPF var host = CreateHostBuilder(e.Args).Build();//生成宿主。 ServiceProvider = host.Services; - + await host.StartAsync(); - + serilogHelper = ServiceProvider.GetService(); var appConfig = host.Services.GetService(); - var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; - Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}"); - + var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; + serilogHelper.Info($"系统初始化完成,日志存放路径:{appConfig.logPath}"); } /// diff --git a/SlnMesnac.WPF/MainWindow.xaml b/SlnMesnac.WPF/MainWindow.xaml index 150870a..babdec9 100644 --- a/SlnMesnac.WPF/MainWindow.xaml +++ b/SlnMesnac.WPF/MainWindow.xaml @@ -6,55 +6,27 @@ xmlns:local="clr-namespace:SlnMesnac.WPF" mc:Ignorable="d" Title="MainWindow" Height="1080" Width="1920" - WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize" Topmost="False"> - + WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize" Topmost="False" Background="#F4F5FA"> + - - - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - + + @@ -68,9 +40,9 @@