using Nop.Core; using Nop.Core.Domain.Common; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Logging; using Nop.Data; namespace Nop.Services.Logging; /// /// Default logger /// public partial class DefaultLogger : ILogger { #region Fields protected readonly CommonSettings _commonSettings; protected readonly CustomerSettings _customerSettings; protected readonly IRepository _logRepository; protected readonly IWebHelper _webHelper; #endregion #region Ctor public DefaultLogger(CommonSettings commonSettings, CustomerSettings customerSettings, IRepository logRepository, IWebHelper webHelper) { _commonSettings = commonSettings; _customerSettings = customerSettings; _logRepository = logRepository; _webHelper = webHelper; } #endregion #region Utilities /// /// Gets a value indicating whether this message should not be logged /// /// Message /// Result protected virtual bool IgnoreLog(string message) { if (!_commonSettings.IgnoreLogWordlist.Any()) return false; if (string.IsNullOrWhiteSpace(message)) return false; return _commonSettings .IgnoreLogWordlist .Any(x => message.Contains(x, StringComparison.InvariantCultureIgnoreCase)); } /// /// Prepare log item /// /// Log level /// The short message /// The full message /// The customer to associate log record with /// Log item protected virtual Log PrepareLog(LogLevel logLevel, string shortMessage, string fullMessage = "", Customer customer = null) { return new Log { LogLevel = logLevel, ShortMessage = shortMessage, FullMessage = fullMessage, IpAddress = _customerSettings.StoreIpAddresses ? _webHelper.GetCurrentIpAddress() : string.Empty, CustomerId = customer?.Id, PageUrl = _webHelper.GetThisPageUrl(true), ReferrerUrl = _webHelper.GetUrlReferrer(), CreatedOnUtc = DateTime.UtcNow }; } #endregion #region Methods /// /// Determines whether a log level is enabled /// /// Log level /// Result public virtual bool IsEnabled(LogLevel level) { return level switch { LogLevel.Debug => false, _ => true, }; } /// /// Deletes a log item /// /// Log item /// A task that represents the asynchronous operation public virtual async Task DeleteLogAsync(Log log) { ArgumentNullException.ThrowIfNull(log); await _logRepository.DeleteAsync(log, false); } /// /// Deletes a log items /// /// Log items /// A task that represents the asynchronous operation public virtual async Task DeleteLogsAsync(IList logs) { await _logRepository.DeleteAsync(logs, false); } /// /// Clears a log /// /// The date that sets the restriction on deleting records. Leave null to remove all records /// A task that represents the asynchronous operation public virtual async Task ClearLogAsync(DateTime? olderThan = null) { if (olderThan == null) await _logRepository.TruncateAsync(); else await _logRepository.DeleteAsync(p => p.CreatedOnUtc < olderThan.Value); } /// /// Gets all log items /// /// Log item creation from; null to load all records /// Log item creation to; null to load all records /// Message /// Log level; null to load all records /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the log item items /// public virtual async Task> GetAllLogsAsync(DateTime? fromUtc = null, DateTime? toUtc = null, string message = "", LogLevel? logLevel = null, int pageIndex = 0, int pageSize = int.MaxValue) { var logs = await _logRepository.GetAllPagedAsync(query => { if (fromUtc.HasValue) query = query.Where(l => fromUtc.Value <= l.CreatedOnUtc); if (toUtc.HasValue) query = query.Where(l => toUtc.Value >= l.CreatedOnUtc); if (logLevel.HasValue) { var logLevelId = (int)logLevel.Value; query = query.Where(l => logLevelId == l.LogLevelId); } if (!string.IsNullOrEmpty(message)) query = query.Where(l => l.ShortMessage.Contains(message) || l.FullMessage.Contains(message)); query = query.OrderByDescending(l => l.CreatedOnUtc); return query; }, pageIndex, pageSize); return logs; } /// /// Gets a log item /// /// Log item identifier /// /// A task that represents the asynchronous operation /// The task result contains the log item /// public virtual async Task GetLogByIdAsync(int logId) { return await _logRepository.GetByIdAsync(logId); } /// /// Get log items by identifiers /// /// Log item identifiers /// /// A task that represents the asynchronous operation /// The task result contains the log items /// public virtual async Task> GetLogByIdsAsync(int[] logIds) { return await _logRepository.GetByIdsAsync(logIds); } /// /// Inserts a log item /// /// Log level /// The short message /// The full message /// The customer to associate log record with /// /// A task that represents the asynchronous operation /// public virtual async Task InsertLogAsync(LogLevel logLevel, string shortMessage, string fullMessage = "", Customer customer = null) { //check ignore word/phrase list? if (IgnoreLog(shortMessage) || IgnoreLog(fullMessage)) return; await _logRepository.InsertAsync(PrepareLog(logLevel, shortMessage, fullMessage, customer), false); } /// /// Inserts a log item /// /// Log level /// The short message /// The full message /// The customer to associate log record with public virtual void InsertLog(LogLevel logLevel, string shortMessage, string fullMessage = "", Customer customer = null) { //check ignore word/phrase list? if (IgnoreLog(shortMessage) || IgnoreLog(fullMessage)) return; _logRepository.Insert(PrepareLog(logLevel, shortMessage, fullMessage, customer), false); } /// /// Information /// /// Message /// Exception /// Customer /// A task that represents the asynchronous operation public virtual async Task InformationAsync(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Information)) await InsertLogAsync(LogLevel.Information, message, exception?.ToString() ?? string.Empty, customer); } /// /// Information /// /// Message /// Exception /// Customer public virtual void Information(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Information)) InsertLog(LogLevel.Information, message, exception?.ToString() ?? string.Empty, customer); } /// /// Warning /// /// Message /// Exception /// Customer /// A task that represents the asynchronous operation public virtual async Task WarningAsync(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Warning)) await InsertLogAsync(LogLevel.Warning, message, exception?.ToString() ?? string.Empty, customer); } /// /// Warning /// /// Message /// Exception /// Customer public virtual void Warning(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Warning)) InsertLog(LogLevel.Warning, message, exception?.ToString() ?? string.Empty, customer); } /// /// Error /// /// Message /// Exception /// Customer /// A task that represents the asynchronous operation public virtual async Task ErrorAsync(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Error)) await InsertLogAsync(LogLevel.Error, message, exception?.ToString() ?? string.Empty, customer); } /// /// Error /// /// Message /// Exception /// Customer public virtual void Error(string message, Exception exception = null, Customer customer = null) { //don't log thread abort exception if (exception is ThreadAbortException) return; if (IsEnabled(LogLevel.Error)) InsertLog(LogLevel.Error, message, exception?.ToString() ?? string.Empty, customer); } #endregion }