😎优化日志相关
This commit is contained in:
parent
fe5211e5fa
commit
2294d7aa62
@ -4,8 +4,9 @@
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using Furion.EventBus;
|
||||
using Furion.Localization;
|
||||
using Furion.Logging;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Admin.NET.Application;
|
||||
|
||||
@ -17,11 +18,13 @@ public class TestService : IDynamicApiController
|
||||
{
|
||||
private readonly UserManager _userManager;
|
||||
private readonly IEventPublisher _eventPublisher;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public TestService(UserManager userManager, IEventPublisher eventPublisher)
|
||||
public TestService(UserManager userManager, IEventPublisher eventPublisher, ILoggerFactory loggerFactory)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_eventPublisher = eventPublisher;
|
||||
_logger = loggerFactory.CreateLogger(CommonConst.SysLogCategoryName); // 日志过滤标识(会写入数据库)
|
||||
}
|
||||
|
||||
[HttpGet("helloWord")]
|
||||
@ -51,4 +54,17 @@ public class TestService : IDynamicApiController
|
||||
|
||||
return L.Text["Admin.NET 通用权限开发平台"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志测试
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void LogTest()
|
||||
{
|
||||
Log.Information("Information");
|
||||
Log.Warning("Warning");
|
||||
Log.Error("Error");
|
||||
|
||||
_logger.LogWarning($"信息{DateTime.Now}");
|
||||
}
|
||||
}
|
||||
@ -56,7 +56,7 @@
|
||||
<PackageReference Include="SSH.NET" Version="2024.2.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1222" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1223" />
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -33,6 +33,11 @@ public class LogJob(IServiceScopeFactory serviceScopeFactory) : IJob
|
||||
// 删除作业触发器运行记录
|
||||
await db.Deleteable<SysJobTriggerRecord>().Where(u => u.CreatedTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
LoggingWriter.LogInformation($"【定时任务】清理系统日志成功,清理 {daysAgo} 天前的日志数据 {DateTime.Now}");
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var message = $"【定时任务】清理系统日志成功,清理 {daysAgo} 天前的日志数据 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
}
|
||||
@ -234,6 +234,7 @@ public class MqttHostedService(IOptions<MqttOptions> mqttOptions) : IHostedServi
|
||||
protected void Logging(string msg)
|
||||
{
|
||||
if (!_mqttOptions.Logging) return;
|
||||
LoggingWriter.LogInformation(msg);
|
||||
Console.WriteLine(msg);
|
||||
Log.Information(msg);
|
||||
}
|
||||
}
|
||||
@ -21,6 +21,12 @@ public class OnlineUserJob(IServiceScopeFactory serviceScopeFactory) : IJob
|
||||
|
||||
var sysOnlineUserService = serviceScope.ServiceProvider.GetRequiredService<SysOnlineUserService>();
|
||||
await sysOnlineUserService.ClearOnline();
|
||||
LoggingWriter.LogInformation($"【定时任务】清理系统在线用户 {DateTime.Now}");
|
||||
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var message = $"【定时任务】清理系统在线用户 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
}
|
||||
@ -19,19 +19,30 @@ public class StartHostedService(IServiceScopeFactory serviceScopeFactory) : IHos
|
||||
{
|
||||
using var serviceScope = _serviceScopeFactory.CreateScope();
|
||||
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
|
||||
// 枚举转字典
|
||||
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||||
await sysEnumService.EnumToDict();
|
||||
LoggingWriter.LogInformation($"【启动任务】系统枚举转换字典 {DateTime.Now}");
|
||||
var message = $"【启动任务】系统枚举转换字典 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
|
||||
// 缓存租户列表
|
||||
await serviceScope.ServiceProvider.GetRequiredService<SysTenantService>().CacheTenant();
|
||||
LoggingWriter.LogInformation($"【启动任务】缓存系统租户列表 {DateTime.Now}");
|
||||
message = $"【启动任务】缓存系统租户列表 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
|
||||
// 清理在线用户
|
||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||
await db.Deleteable<SysOnlineUser>().ExecuteCommandAsync(cancellationToken);
|
||||
LoggingWriter.LogInformation($"【启动任务】清理系统在线用户 {DateTime.Now}");
|
||||
message = $"【启动任务】清理系统在线用户 {DateTime.Now}";
|
||||
Console.WriteLine(message);
|
||||
Log.Information(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
@ -17,8 +17,8 @@ public static class LoggingSetup
|
||||
// 日志监听
|
||||
services.AddMonitorLogging(options =>
|
||||
{
|
||||
options.IgnorePropertyNames = new[] { "Byte" };
|
||||
options.IgnorePropertyTypes = new[] { typeof(byte[]) };
|
||||
options.IgnorePropertyNames = ["Byte"];
|
||||
options.IgnorePropertyTypes = [typeof(byte[])];
|
||||
});
|
||||
|
||||
// 控制台日志
|
||||
@ -38,7 +38,7 @@ public static class LoggingSetup
|
||||
if (App.GetConfig<bool>("Logging:File:Enabled", true))
|
||||
{
|
||||
var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);
|
||||
Array.ForEach(new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error }, logLevel =>
|
||||
Array.ForEach([LogLevel.Information, LogLevel.Warning, LogLevel.Error], logLevel =>
|
||||
{
|
||||
services.AddFileLogging(options =>
|
||||
{
|
||||
|
||||
@ -1,60 +0,0 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 系统日志写入器
|
||||
/// </summary>
|
||||
public static class LoggingWriter
|
||||
{
|
||||
private static readonly ILogger _logger = Log.CreateLoggerFactory().CreateLogger(CommonConst.SysLogCategoryName);
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统消息日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogInformation(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogInformation(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统警告日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogWarning(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统错误日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogError(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogWarning(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
}
|
||||
@ -115,6 +115,7 @@ public class MqttEventInterceptor
|
||||
protected static void Logging(string msg)
|
||||
{
|
||||
if (!App.GetOptions<MqttOptions>().Logging) return;
|
||||
LoggingWriter.LogInformation(msg);
|
||||
Console.WriteLine(msg);
|
||||
Log.Information(msg);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user