bklLiudl
2024-07-12 0c4060335e5baa452f510f3d7973c8cb995aa97d
Merge branch 'master' into Liudl
2个文件已修改
85 ■■■■■ 已修改文件
Wms/Utility/Extension/ApplicationBuilderExtensions.cs 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Wms/Utility/Extension/ServiceCollectionExtensions.cs 73 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Wms/Utility/Extension/ApplicationBuilderExtensions.cs
@@ -71,6 +71,10 @@
                        }
                    }
                    else
                    {
                        diagnosticContext.Set("_UserName", "");
                    }
                    //请求body
                    var requestContent = "{}";
                    var method = httpContext.Request.Method.ToLower();
@@ -81,6 +85,14 @@
                        if (requestReader.BaseStream.Length != 0)
                            requestContent = requestReader.ReadToEnd();
                    }
                    if (httpContext.Request.RouteValues["controller"].ToString().ToLower() == "upapi"|| httpContext.Request.RouteValues["controller"].ToString().ToLower() == "downapi")
                    {
                        diagnosticContext.Set("ExternalSystems", true);
                    }
                    else
                    {
                        diagnosticContext.Set("ExternalSystems", false);
                    }
                    diagnosticContext.Set("_RequestBody", requestContent);
                    diagnosticContext.Set("_Service", AppDomain.CurrentDomain.FriendlyName);
                };
Wms/Utility/Extension/ServiceCollectionExtensions.cs
@@ -7,11 +7,20 @@
using System.Text;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog.Filters;
using Autofac.Core;
using Serilog.Formatting.Json;
using System.Linq;
using ZXing;
using SqlSugar.Extensions;
namespace Utility
{
    public static class ServiceCollectionExtensions
    {
        internal static string LogFilePath(string fileName) => $@"Logs/{fileName}/{DateTime.Now.Year}.{DateTime.Now.Month}/log_.log";
        internal static readonly string seriCustomProperty = "seriPos";
        internal static readonly string logOutputTemplate = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
        internal static readonly long? fileSize = 31457280L;
        const string template = "时间: {Timestamp:yyyy-MM-dd HH:mm:ss}{NewLine}来源: {SourceContext}{NewLine}内容: [{Level:u3}] {Message}{NewLine}{Exception}{NewLine}";
        /// <summary>
        /// 添加配置Serilog
@@ -21,9 +30,8 @@
        /// <returns></returns>
        public static IServiceCollection AddConfigSerilog(this IServiceCollection services)
        {
            // 创建Serilog记录日志
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Fatal)
                .MinimumLevel.Override("Quartz", LogEventLevel.Warning)
@@ -33,42 +41,35 @@
                .WriteTo.Async(c => c.Console(
                   theme: AnsiConsoleTheme.Literate,
                    outputTemplate: template))
                // Debug日志写入到文件
                //.WriteTo.Async(c => c.File(
                //    path: "Logs/Debug_.txt",
                //    rollingInterval: RollingInterval.Day,
                //    fileSizeLimitBytes: 1024 * 1024 * 10,
                //    retainedFileCountLimit: 100,
                //    outputTemplate: template,
                //    restrictedToMinimumLevel: LogEventLevel.Debug))
                // Information日志写入到文件
                .WriteTo.Async(c => c.File(
                    path: "Logs/Information_.txt",
                    rollingInterval: RollingInterval.Day,
                    fileSizeLimitBytes: 1024 * 1024 * 10,
                    retainedFileCountLimit: 100,
                    outputTemplate: template,
                    restrictedToMinimumLevel: LogEventLevel.Information))
                // Warning日志写入到文件
                .WriteTo.Async(c => c.File(
                    path: "Logs/Warning_.txt",
                    rollingInterval: RollingInterval.Day,
                    fileSizeLimitBytes: 1024 * 1024 * 10,
                    retainedFileCountLimit: 100,
                    outputTemplate: template,
                    restrictedToMinimumLevel: LogEventLevel.Warning))
                // Error日志写入到文件
                .WriteTo.Async(c => c.File(
                    path: "Logs/Error_.txt",
                    rollingInterval: RollingInterval.Day,
                    fileSizeLimitBytes: 1024 * 1024 * 10,
                    retainedFileCountLimit: 1000,
                    outputTemplate: template,
                    restrictedToMinimumLevel: LogEventLevel.Error))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Information)
                        .WriteTo.File("Logs/info/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Debug)
                        .WriteTo.File("Logs/debug/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Error)
                        .WriteTo.File("Logs/error/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Verbose)
                        .WriteTo.File("Logs/verbose/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) => e.Level == Serilog.Events.LogEventLevel.Warning)
                        .WriteTo.File("Logs/warning/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .WriteTo.Logger(lc => lc
                        .Filter.ByIncludingOnly((e) =>
                        {
                            var result = e.Properties.FirstOrDefault(c => c.Key == "ExternalSystems").Value;
                            if (result != null)
                            {
                                return result.ObjToBool();
                            }
                            return false;
                        })
                        .WriteTo.File("Logs/api/.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 10))
                .CreateLogger();
            services.AddSerilog();
            services.AddSerilog(Log.Logger);
            return services;
        }
    }