You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
5.5 KiB
C#

1 year ago
using Microsoft.AspNetCore.Builder;
using Serilog;
using Serilog.Events;
using SlnMesnac.Config;
using System;
using System.IO;
#region << 版 本 注 释 >>
2 months ago
1 year ago
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* LAPTOP-E0N2L34V
* SlnMesnac.Serilog
* 496f8d2b-70e3-4a05-ae18-a9b0fcd06b82
*
* WenJY
* wenjy@mesnac.com
* 2024-03-27 21:58:35
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
2 months ago
1 year ago
#endregion << 版 本 注 释 >>
2 months ago
1 year ago
namespace SlnMesnac.Serilog
{
/// <summary>
/// Serilog
/// </summary>
public static class SerilogExtensions
{
2 months ago
// 定义自定义日志级别
public static readonly LogEventLevel AgvLevel = (LogEventLevel)35;
//public static IApplicationBuilder UseSerilogExtensions(this IApplicationBuilder app)
//{
// // 获取当前程序所在的目录
// string currentDirectory = Directory.GetCurrentDirectory();
// Log.Logger = new LoggerConfiguration()
// .MinimumLevel.Verbose() // 确保能捕获所有级别日志
// .Enrich.With(new CustomLogLevelAbbreviationEnricher()) // 添加自定义日志级别缩写增强器
// .WriteTo.Console(
// outputTemplate: "{Timestamp:MM-dd HH:mm:ss} [{LevelAbbreviation}] [{ClassName}] [{LineNumber}] {Message:lj}{NewLine}{Exception}",
// restrictedToMinimumLevel: LogEventLevel.Error
// )
// // retainedFileCountLimit: 3, // 保留最近3天日志
// .WriteTo.File(path: GetDynamicLogPath("Info"), retainedFileCountLimit: 3, restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] [{ClassName}] [{LineNumber}] {Message:lj}{NewLine}{Exception}")
// .WriteTo.File(path: GetDynamicLogPath("Error"), retainedFileCountLimit: 3, restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] [{ClassName}] [{LineNumber}] {Message:lj}{NewLine}{Exception}")
// .WriteTo.File(path: GetDynamicLogPath("Agv"), retainedFileCountLimit: 3, restrictedToMinimumLevel: AgvLevel, rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] [{ClassName}] [{LineNumber}] {Message:lj}{NewLine}{Exception}") // 新增 Agv 日志文件
// .CreateLogger();
// // 启用 Serilog 中间件
// app.UseSerilogRequestLogging();
// return app;
//}
1 year ago
public static IApplicationBuilder UseSerilogExtensions(this IApplicationBuilder app)
{
2 months ago
// 获取当前程序所在的目录
string currentDirectory = Directory.GetCurrentDirectory();
var logPath = Path.Combine(currentDirectory, "Logs");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose() // 确保能捕获所有级别日志
.Enrich.With(new CustomLogLevelAbbreviationEnricher()) // 添加自定义日志级别缩写增强器
.WriteTo.Console(
outputTemplate: "{Timestamp:MM-dd HH:mm:ss} [{LevelAbbreviation}] [{ClassName}] [{LineNumber}] {Message:lj}{NewLine}{Exception}",
restrictedToMinimumLevel: LogEventLevel.Error
)
// retainedFileCountLimit: 3, // 保留最近3天日志
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Info"))
.WriteTo.File(Path.Combine($"{logPath}/Info/", "Info.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Plc"))
.WriteTo.File(Path.Combine($"{logPath}/Plc/", "Plc.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Agv"))
.WriteTo.File(Path.Combine($"{logPath}/Agv/", "Agv.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey("Module") && logEvent.Properties["Module"].ToString().Contains("Error"))
.WriteTo.File(Path.Combine($"{logPath}/Error/", "Error.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 3))
.CreateLogger();
1 year ago
2 months ago
// 启用 Serilog 中间件
1 year ago
app.UseSerilogRequestLogging();
return app;
}
2 months ago
// 新的动态路径生成方法
private static string GetDynamicLogPath(string logType)
{
return Path.Combine(
Directory.GetCurrentDirectory(),
"Logs",
$"{DateTime.UtcNow:yyyy-MM-dd}",
$"{logType}-.log"
);
}
1 year ago
}
2 months ago
}