调整
This commit is contained in:
parent
d784357fe5
commit
66756b555f
@ -1,11 +0,0 @@
|
|||||||
namespace BangumiRenamer.Config;
|
|
||||||
|
|
||||||
public class ConfigItemAttribute : System.Attribute
|
|
||||||
{
|
|
||||||
public string Name;
|
|
||||||
|
|
||||||
public ConfigItemAttribute(string name)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
namespace BangumiRenamer.Config;
|
|
||||||
|
|
||||||
public interface IConfigItem;
|
|
||||||
@ -1,7 +1,9 @@
|
|||||||
namespace BangumiRenamer.Config;
|
using BangumiRenamer.Utils;
|
||||||
|
|
||||||
[ConfigItem("Proxy")]
|
namespace BangumiRenamer.ConfigSchema;
|
||||||
public class ProxyConfig : IConfigItem
|
|
||||||
{
|
[ConfigItem("Proxy")]
|
||||||
public string HttpProxy = "";
|
public class ProxyConfig : IConfigItem
|
||||||
|
{
|
||||||
|
public string HttpProxy = "";
|
||||||
}
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
namespace BangumiRenamer.Config;
|
using BangumiRenamer.Utils;
|
||||||
|
|
||||||
[ConfigItem("TMDB")]
|
namespace BangumiRenamer.Config;
|
||||||
public class TMDBConfig : IConfigItem
|
|
||||||
{
|
[ConfigItem("TMDB")]
|
||||||
public string ApiKey = "";
|
public class TMDBConfig : IConfigItem
|
||||||
|
{
|
||||||
|
public string ApiKey = "";
|
||||||
}
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using BangumiRenamer.Utils;
|
||||||
using NativeFileDialogSharp;
|
using NativeFileDialogSharp;
|
||||||
|
|
||||||
namespace BangumiRenamer.Tools;
|
namespace BangumiRenamer.Tools;
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
|
using BangumiRenamer.Config;
|
||||||
|
using BangumiRenamer.ConfigSchema;
|
||||||
|
|
||||||
namespace BangumiRenamer.Tools;
|
namespace BangumiRenamer.Tools;
|
||||||
|
|
||||||
using Config;
|
using Utils;
|
||||||
using TMDbLib.Client;
|
using TMDbLib.Client;
|
||||||
using Data;
|
using Data;
|
||||||
using NativeFileDialogSharp;
|
using NativeFileDialogSharp;
|
||||||
|
|||||||
@ -1,8 +1,21 @@
|
|||||||
namespace BangumiRenamer.Config;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace BangumiRenamer.Utils;
|
||||||
|
|
||||||
|
public class ConfigItemAttribute : System.Attribute
|
||||||
|
{
|
||||||
|
public string Name;
|
||||||
|
|
||||||
|
public ConfigItemAttribute(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IConfigItem;
|
||||||
|
|
||||||
public class Config(string configPath)
|
public class Config(string configPath)
|
||||||
{
|
{
|
||||||
private static readonly Lazy<Config> _lazy = new (() =>
|
private static readonly Lazy<Config> _lazy = new (() =>
|
||||||
@ -1,164 +1,162 @@
|
|||||||
using Serilog.Sinks.SystemConsole.Themes;
|
using Serilog;
|
||||||
|
using Serilog.Core;
|
||||||
namespace BangumiRenamer;
|
using Serilog.Events;
|
||||||
|
using Serilog.Sinks.SystemConsole.Themes;
|
||||||
using Serilog;
|
|
||||||
using Serilog.Core;
|
namespace BangumiRenamer.Utils;
|
||||||
using Serilog.Events;
|
|
||||||
|
public sealed class Log
|
||||||
public sealed class Log
|
{
|
||||||
{
|
private static readonly Lazy<Log> _instance = new Lazy<Log>(() => new Log());
|
||||||
private static readonly Lazy<Log> _instance = new Lazy<Log>(() => new Log());
|
private Logger _logger;
|
||||||
private Logger _logger;
|
private bool _isInitialized = false;
|
||||||
private bool _isInitialized = false;
|
private readonly object _lockObject = new object();
|
||||||
private readonly object _lockObject = new object();
|
|
||||||
|
public static Log Instance => _instance.Value;
|
||||||
public static Log Instance => _instance.Value;
|
public static ILogger Logger => Instance._logger;
|
||||||
public static ILogger Logger => Instance._logger;
|
|
||||||
|
private Log() { }
|
||||||
private Log() { }
|
|
||||||
|
/// <summary>
|
||||||
/// <summary>
|
/// 初始化日志配置
|
||||||
/// 初始化日志配置
|
/// </summary>
|
||||||
/// </summary>
|
public static void Initialize(Action<LoggerConfiguration> configure = null)
|
||||||
public static void Initialize(Action<LoggerConfiguration> configure = null)
|
{
|
||||||
{
|
Instance.InitializeInternal(configure);
|
||||||
Instance.InitializeInternal(configure);
|
}
|
||||||
}
|
|
||||||
|
private void InitializeInternal(Action<LoggerConfiguration> configure = null)
|
||||||
private void InitializeInternal(Action<LoggerConfiguration> configure = null)
|
{
|
||||||
{
|
lock (_lockObject)
|
||||||
lock (_lockObject)
|
{
|
||||||
{
|
if (_isInitialized)
|
||||||
if (_isInitialized)
|
{
|
||||||
{
|
Warn("日志系统已经初始化过");
|
||||||
Warn("日志系统已经初始化过");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
var config = new LoggerConfiguration()
|
||||||
var config = new LoggerConfiguration()
|
.MinimumLevel.Debug()
|
||||||
.MinimumLevel.Debug()
|
.WriteTo.Console(
|
||||||
.WriteTo.Console(
|
outputTemplate: "[{Timestamp:HH:mm:ss}][{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
||||||
outputTemplate: "[{Timestamp:HH:mm:ss}][{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
theme: AnsiConsoleTheme.Sixteen
|
||||||
theme: AnsiConsoleTheme.Sixteen
|
)
|
||||||
)
|
.Enrich.FromLogContext()
|
||||||
.Enrich.FromLogContext()
|
.Enrich.WithThreadId();
|
||||||
.Enrich.WithProperty("Application", "MyApp")
|
|
||||||
.Enrich.WithThreadId();
|
configure?.Invoke(config);
|
||||||
|
|
||||||
configure?.Invoke(config);
|
_logger = config.CreateLogger();
|
||||||
|
_isInitialized = true;
|
||||||
_logger = config.CreateLogger();
|
|
||||||
_isInitialized = true;
|
Info("日志系统初始化完成");
|
||||||
|
}
|
||||||
Info("日志系统初始化完成");
|
}
|
||||||
}
|
|
||||||
}
|
// 静态快捷方法 - 使用更简短的名称
|
||||||
|
public static void Debug(string message, params object[] properties)
|
||||||
// 静态快捷方法 - 使用更简短的名称
|
=> Instance.EnsureAndLog(LogEventLevel.Debug, message, properties);
|
||||||
public static void Debug(string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(LogEventLevel.Debug, message, properties);
|
public static void Info(string message, params object[] properties)
|
||||||
|
=> Instance.EnsureAndLog(LogEventLevel.Information, message, properties);
|
||||||
public static void Info(string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(LogEventLevel.Information, message, properties);
|
public static void Warn(string message, params object[] properties)
|
||||||
|
=> Instance.EnsureAndLog(LogEventLevel.Warning, message, properties);
|
||||||
public static void Warn(string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(LogEventLevel.Warning, message, properties);
|
public static void Error(string message, params object[] properties)
|
||||||
|
=> Instance.EnsureAndLog(LogEventLevel.Error, message, properties);
|
||||||
public static void Error(string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(LogEventLevel.Error, message, properties);
|
public static void Error(Exception ex, string message, params object[] properties)
|
||||||
|
=> Instance.EnsureAndLog(ex, message, properties);
|
||||||
public static void Error(Exception ex, string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(ex, message, properties);
|
public static void Fatal(string message, params object[] properties)
|
||||||
|
=> Instance.EnsureAndLog(LogEventLevel.Fatal, message, properties);
|
||||||
public static void Fatal(string message, params object[] properties)
|
|
||||||
=> Instance.EnsureAndLog(LogEventLevel.Fatal, message, properties);
|
// 实例方法
|
||||||
|
private void EnsureAndLog(LogEventLevel level, string message, object[] properties)
|
||||||
// 实例方法
|
{
|
||||||
private void EnsureAndLog(LogEventLevel level, string message, object[] properties)
|
EnsureInitialized();
|
||||||
{
|
_logger.Write(level, message, properties);
|
||||||
EnsureInitialized();
|
}
|
||||||
_logger.Write(level, message, properties);
|
|
||||||
}
|
private void EnsureAndLog(Exception ex, string message, object[] properties)
|
||||||
|
{
|
||||||
private void EnsureAndLog(Exception ex, string message, object[] properties)
|
EnsureInitialized();
|
||||||
{
|
_logger.Error(ex, message, properties);
|
||||||
EnsureInitialized();
|
}
|
||||||
_logger.Error(ex, message, properties);
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 为特定类型创建Logger
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// 为特定类型创建Logger
|
public static ILogger ForContext<T>()
|
||||||
/// </summary>
|
{
|
||||||
public static ILogger ForContext<T>()
|
Instance.EnsureInitialized();
|
||||||
{
|
return Instance._logger.ForContext<T>();
|
||||||
Instance.EnsureInitialized();
|
}
|
||||||
return Instance._logger.ForContext<T>();
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 为特定源创建Logger
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// 为特定源创建Logger
|
public static ILogger ForContext(string source)
|
||||||
/// </summary>
|
{
|
||||||
public static ILogger ForContext(string source)
|
Instance.EnsureInitialized();
|
||||||
{
|
return Instance._logger.ForContext("SourceContext", source);
|
||||||
Instance.EnsureInitialized();
|
}
|
||||||
return Instance._logger.ForContext("SourceContext", source);
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 开始一个带属性的日志上下文
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// 开始一个带属性的日志上下文
|
public static IDisposable BeginScope(string propertyName, object value)
|
||||||
/// </summary>
|
{
|
||||||
public static IDisposable BeginScope(string propertyName, object value)
|
Instance.EnsureInitialized();
|
||||||
{
|
return Serilog.Context.LogContext.PushProperty(propertyName, value);
|
||||||
Instance.EnsureInitialized();
|
}
|
||||||
return Serilog.Context.LogContext.PushProperty(propertyName, value);
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 开始多个属性的日志上下文
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// 开始多个属性的日志上下文
|
public static IDisposable BeginScope(params (string Name, object Value)[] properties)
|
||||||
/// </summary>
|
{
|
||||||
public static IDisposable BeginScope(params (string Name, object Value)[] properties)
|
Instance.EnsureInitialized();
|
||||||
{
|
var disposables = properties.Select(p =>
|
||||||
Instance.EnsureInitialized();
|
Serilog.Context.LogContext.PushProperty(p.Name, p.Value)).ToArray();
|
||||||
var disposables = properties.Select(p =>
|
return new DisposableGroup(disposables);
|
||||||
Serilog.Context.LogContext.PushProperty(p.Name, p.Value)).ToArray();
|
}
|
||||||
return new DisposableGroup(disposables);
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 关闭并刷新日志
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// 关闭并刷新日志
|
public static void Close()
|
||||||
/// </summary>
|
{
|
||||||
public static void Close()
|
Instance._logger?.Dispose();
|
||||||
{
|
Serilog.Log.CloseAndFlush();
|
||||||
Instance._logger?.Dispose();
|
Instance._isInitialized = false;
|
||||||
Serilog.Log.CloseAndFlush();
|
}
|
||||||
Instance._isInitialized = false;
|
|
||||||
}
|
private void EnsureInitialized()
|
||||||
|
{
|
||||||
private void EnsureInitialized()
|
if (!_isInitialized)
|
||||||
{
|
{
|
||||||
if (!_isInitialized)
|
InitializeInternal();
|
||||||
{
|
}
|
||||||
InitializeInternal();
|
}
|
||||||
}
|
|
||||||
}
|
// 辅助类:用于同时释放多个IDisposable
|
||||||
|
private class DisposableGroup : IDisposable
|
||||||
// 辅助类:用于同时释放多个IDisposable
|
{
|
||||||
private class DisposableGroup : IDisposable
|
private readonly IDisposable[] _disposables;
|
||||||
{
|
|
||||||
private readonly IDisposable[] _disposables;
|
public DisposableGroup(IDisposable[] disposables)
|
||||||
|
{
|
||||||
public DisposableGroup(IDisposable[] disposables)
|
_disposables = disposables;
|
||||||
{
|
}
|
||||||
_disposables = disposables;
|
|
||||||
}
|
public void Dispose()
|
||||||
|
{
|
||||||
public void Dispose()
|
foreach (var disposable in _disposables)
|
||||||
{
|
{
|
||||||
foreach (var disposable in _disposables)
|
disposable?.Dispose();
|
||||||
{
|
}
|
||||||
disposable?.Dispose();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user