修复日志不打印问题
This commit is contained in:
@@ -24,6 +24,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using NLog.Web;
|
||||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||||
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
||||||
|
|
||||||
@@ -94,6 +95,7 @@ public partial class App : System.Windows.Application
|
|||||||
private void ConfigureServices(IServiceCollection services)
|
private void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
// 注册NLogLogger作为Microsoft.Extensions.Logging.ILogger的实现
|
// 注册NLogLogger作为Microsoft.Extensions.Logging.ILogger的实现
|
||||||
|
services.AddSingleton<ILogger, NLogLogger>();
|
||||||
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
|
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
|
||||||
services.AddSingleton<GrowlNotificationService>();
|
services.AddSingleton<GrowlNotificationService>();
|
||||||
services.AddSingleton<INotificationService, NotificationService>();
|
services.AddSingleton<INotificationService, NotificationService>();
|
||||||
@@ -203,6 +205,7 @@ public partial class App : System.Windows.Application
|
|||||||
LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config");
|
LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config");
|
||||||
loggingBuilder.ClearProviders();
|
loggingBuilder.ClearProviders();
|
||||||
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
||||||
|
// loggingBuilder.addn; // 添加NLog作为日志提供者
|
||||||
|
|
||||||
// 捕获未处理的异常并记录
|
// 捕获未处理的异常并记录
|
||||||
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ namespace DMS.WPF.Logging;
|
|||||||
public class NLogLogger : ILogger
|
public class NLogLogger : ILogger
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取当前类的 NLog 日志实例。
|
/// 获取指定名称的 NLog 日志实例。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
private readonly NLog.Logger _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日志记录器名称
|
/// 日志记录器名称
|
||||||
@@ -27,11 +27,13 @@ public class NLogLogger : ILogger
|
|||||||
public NLogLogger()
|
public NLogLogger()
|
||||||
{
|
{
|
||||||
_name = nameof(NLogLogger);
|
_name = nameof(NLogLogger);
|
||||||
|
_logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public NLogLogger(string name)
|
public NLogLogger(string name)
|
||||||
{
|
{
|
||||||
_name = name;
|
_name = name;
|
||||||
|
_logger = NLog.LogManager.GetLogger(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -75,7 +77,7 @@ public class NLogLogger : ILogger
|
|||||||
// 如果不启用节流,则直接记录日志并返回。
|
// 如果不启用节流,则直接记录日志并返回。
|
||||||
if (!throttle)
|
if (!throttle)
|
||||||
{
|
{
|
||||||
Logger.Log(level, exception, msg);
|
_logger.Log(level, exception, msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +92,7 @@ public class NLogLogger : ILogger
|
|||||||
_ =>
|
_ =>
|
||||||
{
|
{
|
||||||
// 1. 首次出现,立即记录一次原始日志。
|
// 1. 首次出现,立即记录一次原始日志。
|
||||||
Logger.Log(level, exception, msg);
|
_logger.Log(level, exception, msg);
|
||||||
|
|
||||||
// 2. 创建一个新的节流信息对象。
|
// 2. 创建一个新的节流信息对象。
|
||||||
var newThrottledLog = new ThrottledLogInfo
|
var newThrottledLog = new ThrottledLogInfo
|
||||||
@@ -111,7 +113,7 @@ public class NLogLogger : ILogger
|
|||||||
if (finishedLog.Count > 1)
|
if (finishedLog.Count > 1)
|
||||||
{
|
{
|
||||||
var summaryMsg = $"日志 '{msg}' 在过去 {ThrottleTimeSeconds} 秒内被调用 {finishedLog.Count} 次。";
|
var summaryMsg = $"日志 '{msg}' 在过去 {ThrottleTimeSeconds} 秒内被调用 {finishedLog.Count} 次。";
|
||||||
Logger.Log(level, summaryMsg);
|
_logger.Log(level, summaryMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, null, ThrottleTimeSeconds * 1000, Timeout.Infinite); // 设置30秒后触发,且不重复。
|
}, null, ThrottleTimeSeconds * 1000, Timeout.Infinite); // 设置30秒后触发,且不重复。
|
||||||
@@ -134,7 +136,7 @@ public class NLogLogger : ILogger
|
|||||||
|
|
||||||
public bool IsEnabled(LogLevel logLevel)
|
public bool IsEnabled(LogLevel logLevel)
|
||||||
{
|
{
|
||||||
return Logger.IsEnabled(ToNLogLevel(logLevel));
|
return _logger.IsEnabled(ToNLogLevel(logLevel));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||||
|
|||||||
Reference in New Issue
Block a user