重构项目结构,引入 Avalonia-Common、Avalonia-EFCore、Avalonia-Services,实现 API 与桌面端统一端点注册、过滤器、鉴权和标准响应格式。支持多数据库自动迁移与配置,集成 Serilog 日志系统。移除旧路由与控制器,提升接口一致性与可维护性。
125 lines
5.0 KiB
C#
125 lines
5.0 KiB
C#
using Serilog;
|
|
using Serilog.Events;
|
|
|
|
namespace Avalonia_Common.Infrastructure
|
|
{
|
|
/// <summary>
|
|
/// Serilog 日志配置 —— 可在 Avalonia-API 和 Avalonia-PC 中共享。
|
|
/// </summary>
|
|
public static class LoggingConfiguration
|
|
{
|
|
/// <summary>
|
|
/// 默认日志目录
|
|
/// </summary>
|
|
private static readonly string DefaultLogDir = Path.Combine(AppContext.BaseDirectory, "logs");
|
|
|
|
/// <summary>
|
|
/// 创建控制台日志记录器(开发环境)。
|
|
/// </summary>
|
|
public static ILogger CreateConsoleLogger(
|
|
LogEventLevel minimumLevel = LogEventLevel.Debug)
|
|
{
|
|
return new LoggerConfiguration()
|
|
.MinimumLevel.Is(minimumLevel)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console(
|
|
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|
.CreateLogger();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建控制台 + 文件日志记录器。
|
|
/// </summary>
|
|
/// <param name="minimumLevel">最低日志级别</param>
|
|
/// <param name="logDir">日志目录,默认 ./logs</param>
|
|
/// <param name="retainedDays">保留天数</param>
|
|
public static ILogger CreateDefaultLogger(
|
|
LogEventLevel minimumLevel = LogEventLevel.Information,
|
|
string? logDir = null,
|
|
int retainedDays = 30)
|
|
{
|
|
logDir ??= DefaultLogDir;
|
|
|
|
return new LoggerConfiguration()
|
|
.MinimumLevel.Is(minimumLevel)
|
|
//.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
//.MinimumLevel.Override("System", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.Enrich.WithMachineName()
|
|
.Enrich.WithThreadId()
|
|
.WriteTo.Console(
|
|
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|
.WriteTo.File(
|
|
path: Path.Combine(logDir, "log-.txt"),
|
|
rollingInterval: RollingInterval.Day,
|
|
retainedFileCountLimit: retainedDays,
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
|
encoding: System.Text.Encoding.UTF8)
|
|
.CreateLogger();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建只写文件的日志记录器(桌面应用静默模式)。
|
|
/// </summary>
|
|
public static ILogger CreateFileOnlyLogger(
|
|
LogEventLevel minimumLevel = LogEventLevel.Information,
|
|
string? logDir = null,
|
|
int retainedDays = 30)
|
|
{
|
|
logDir ??= DefaultLogDir;
|
|
|
|
return new LoggerConfiguration()
|
|
.MinimumLevel.Is(minimumLevel)
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.File(
|
|
path: Path.Combine(logDir, "app-.txt"),
|
|
rollingInterval: RollingInterval.Day,
|
|
retainedFileCountLimit: retainedDays,
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
|
encoding: System.Text.Encoding.UTF8)
|
|
.CreateLogger();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 静态日志访问器 —— 全局静态入口,方便在没有 DI 的场景下使用。
|
|
/// </summary>
|
|
public static class AppLog
|
|
{
|
|
private static ILogger? _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化全局日志记录器。
|
|
/// </summary>
|
|
public static void Initialize(ILogger logger)
|
|
{
|
|
_logger = logger;
|
|
Log.Logger = logger;
|
|
}
|
|
|
|
public static ILogger Logger => _logger ?? Log.Logger;
|
|
|
|
public static void Debug(string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Debug(messageTemplate, propertyValues);
|
|
|
|
public static void Information(string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Information(messageTemplate, propertyValues);
|
|
|
|
public static void Warning(string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Warning(messageTemplate, propertyValues);
|
|
|
|
public static void Error(string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Error(messageTemplate, propertyValues);
|
|
|
|
public static void Error(Exception exception, string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Error(exception, messageTemplate, propertyValues);
|
|
|
|
public static void Fatal(string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Fatal(messageTemplate, propertyValues);
|
|
|
|
public static void Fatal(Exception exception, string messageTemplate, params object?[] propertyValues)
|
|
=> Logger.Fatal(exception, messageTemplate, propertyValues);
|
|
}
|
|
}
|