From 9895abdb4c0c060c52e285631bf3df845efb31e7 Mon Sep 17 00:00:00 2001
From: hwh <332078369@qq.com>
Date: 星期四, 11 七月 2024 15:44:47 +0800
Subject: [PATCH] 日志优化
---
Wms/Utility/Extension/ApplicationBuilderExtensions.cs | 12 ++++
Wms/Utility/Extension/ServiceCollectionExtensions.cs | 128 +++++++++++++++++++++++++++++++++---------
2 files changed, 113 insertions(+), 27 deletions(-)
diff --git a/Wms/Utility/Extension/ApplicationBuilderExtensions.cs b/Wms/Utility/Extension/ApplicationBuilderExtensions.cs
index d8835b2..3ea0774 100644
--- a/Wms/Utility/Extension/ApplicationBuilderExtensions.cs
+++ b/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);
};
diff --git a/Wms/Utility/Extension/ServiceCollectionExtensions.cs b/Wms/Utility/Extension/ServiceCollectionExtensions.cs
index 0abe4ed..51e880d 100644
--- a/Wms/Utility/Extension/ServiceCollectionExtensions.cs
+++ b/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
@@ -22,8 +31,71 @@
public static IServiceCollection AddConfigSerilog(this IServiceCollection services)
{
// 鍒涘缓Serilog璁板綍鏃ュ織
+ //Log.Logger = new LoggerConfiguration()
+ // .MinimumLevel.Debug()
+ // .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
+ // .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Fatal)
+ // .MinimumLevel.Override("Quartz", LogEventLevel.Warning)
+ // .MinimumLevel.Override("Serilog", LogEventLevel.Information)
+ // // 鍏ㄩ儴鏃ュ織鍐欏叆鍒癈onsole
+ // .WriteTo.Console()
+ // .WriteTo.Async(c => c.Console(
+ // theme: AnsiConsoleTheme.Literate,
+ // outputTemplate: template))
+ // // Debug鏃ュ織鍐欏叆鍒版枃浠�
+ // .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.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.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.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.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.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.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error))
+ // .WriteTo.Async(c => c.File(
+ // path: "Logs/Error_.txt",
+ // rollingInterval: RollingInterval.Day,
+ // fileSizeLimitBytes: 1024 * 1024 * 10,
+ // retainedFileCountLimit: 1000,
+ // outputTemplate: template,
+ // restrictedToMinimumLevel: LogEventLevel.Error))
+ // .CreateLogger();
+
+ //Log.Logger = new LoggerConfiguration()
+ // .Enrich.FromLogContext()
+ // .WriteTo.Console() // 杈撳嚭鍒版帶鍒跺彴
+ // .MinimumLevel.Debug() // 娴嬭瘯鏈�灏忚褰曠骇鍒�
+ // .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => {
+ // var result = p.Properties.FirstOrDefault(c => c.Key == "SourceContext").Value;
+ // if (result != null)
+ // {
+ // return result.ToString().Contains("妯″潡鍚�");
+ // }
+ // return false;
+ // }).WriteTo
+ // .File(LogFilePath("妯″潡鐩綍"), rollingInterval: RollingInterval.Day, outputTemplate: template))
+ // .CreateLogger();
Log.Logger = new LoggerConfiguration()
- .MinimumLevel.Information()
+ .MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Fatal)
.MinimumLevel.Override("Quartz", LogEventLevel.Warning)
@@ -34,6 +106,7 @@
theme: AnsiConsoleTheme.Literate,
outputTemplate: template))
// Debug鏃ュ織鍐欏叆鍒版枃浠�
+ //.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug))
//.WriteTo.Async(c => c.File(
// path: "Logs/Debug_.txt",
// rollingInterval: RollingInterval.Day,
@@ -41,34 +114,35 @@
// 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;
}
}
--
Gitblit v1.8.0