57 lines
2.0 KiB
C#
57 lines
2.0 KiB
C#
using Quartz.Logging;
|
|
|
|
namespace MachineJob
|
|
{
|
|
/// <summary>
|
|
/// 控制台日志提供者
|
|
/// 为 Quartz 调度器提供简单的控制台日志输出
|
|
/// </summary>
|
|
public class ConsoleLogProvider : ILogProvider
|
|
{
|
|
// 配置根对象
|
|
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
|
// 设置配置文件的基础路径
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
// 添加 appsettings.json 配置文件
|
|
.AddJsonFile("appsettings.json")
|
|
// 根据环境变量添加对应的配置文件
|
|
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
|
|
.Build();
|
|
|
|
/// <summary>
|
|
/// 获取日志记录器
|
|
/// </summary>
|
|
/// <param name="name">日志记录器名称</param>
|
|
/// <returns>日志记录器委托</returns>
|
|
public Logger GetLogger(string name)
|
|
{
|
|
return (level, func, exception, parameters) =>
|
|
{
|
|
// 如果日志级别大于等于配置的级别且有消息函数
|
|
if (level >= configuration.GetSection("Quartz").GetValue<Quartz.Logging.LogLevel>("LogLevel") && func != null)
|
|
{
|
|
// 输出到控制台,格式:[时间] [级别] 消息
|
|
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters);
|
|
}
|
|
return true;
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开嵌套上下文(未实现)
|
|
/// </summary>
|
|
public IDisposable OpenNestedContext(string message)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开映射上下文(未实现)
|
|
/// </summary>
|
|
public IDisposable OpenMappedContext(string key, object value, bool destructure = false)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|