158 lines
6.8 KiB
C#
158 lines
6.8 KiB
C#
using AyCode.Core.Helpers;
|
|
using AyCode.Entities;
|
|
using AyCode.Entities.Server.LogItems;
|
|
using AyCode.Utils.Extensions;
|
|
using LinqToDB;
|
|
using LinqToDB.Data;
|
|
using LinqToDB.DataProvider;
|
|
using LinqToDB.DataProvider.SqlServer;
|
|
using Mango.Nop.Data.Repositories;
|
|
using Nop.Core;
|
|
using Nop.Core.Domain.Common;
|
|
using Nop.Core.Domain.Customers;
|
|
using Nop.Core.Domain.Logging;
|
|
using Nop.Data;
|
|
using Nop.Data.DataProviders;
|
|
using System.Data.Common;
|
|
using System.Transactions;
|
|
using LogLevel = AyCode.Core.Loggers.LogLevel;
|
|
using LogLevelNop = Nop.Core.Domain.Logging.LogLevel;
|
|
|
|
namespace Mango.Nop.Services.Loggers
|
|
{
|
|
public interface INopLoggerMsSqlNopDataProvider
|
|
{
|
|
|
|
}
|
|
|
|
public class NopLoggerMsSqlNopDataProvider : MsSqlNopDataProvider, INopLoggerMsSqlNopDataProvider
|
|
{
|
|
//protected override IDataProvider LinqToDbDataProvider => SqlServerTools.GetDataProvider(SqlServerVersion.v2012, SqlServerProvider.MicrosoftDataSqlClient);
|
|
|
|
//protected override DbConnection CreateDbConnection(string connectionString = null)
|
|
//{
|
|
// connectionString = "Data Source=195.26.231.218;Initial Catalog=MangoManagement;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=v6f_?xNfg9N1;Trust Server Certificate=True";
|
|
// return base.GetInternalDbConnection(connectionString);
|
|
//}
|
|
|
|
public void InsertLogItem<TEntity>(TEntity entity) where TEntity : notnull
|
|
{
|
|
using var transaction = CreateTransactionScope(TransactionScopeAsyncFlowOption.Suppress);
|
|
using var dataContext = CreateDataConnection();
|
|
|
|
dataContext.Insert(entity);
|
|
transaction.Complete();
|
|
}
|
|
|
|
public async Task InsertLogItemAsync<TEntity>(TEntity entity) where TEntity : notnull
|
|
{
|
|
using var transaction = CreateTransactionScope(TransactionScopeAsyncFlowOption.Enabled);
|
|
await using var dataContext = CreateDataConnection();
|
|
|
|
await dataContext.InsertAsync(entity);
|
|
transaction.Complete();
|
|
}
|
|
|
|
private static TransactionScope CreateTransactionScope(TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption)
|
|
{
|
|
var transactionOptions = new TransactionOptions
|
|
{
|
|
IsolationLevel = IsolationLevel.ReadUncommitted,
|
|
Timeout = TransactionManager.DefaultTimeout
|
|
};
|
|
|
|
return new TransactionScope(TransactionScopeOption.Suppress, transactionOptions, transactionScopeAsyncFlowOption);
|
|
}
|
|
}
|
|
|
|
public class NopLogWriter : AcLogItemWriterBase<AcLogItem>//AcTextLogWriterBase
|
|
{
|
|
private readonly SemaphoreSlim _lockSlim = new(0);
|
|
|
|
private NopLoggerMsSqlNopDataProvider _dataProvider;
|
|
private readonly global::Nop.Services.Logging.ILogger _nopLogger;
|
|
|
|
protected readonly CommonSettings _commonSettings;
|
|
protected readonly CustomerSettings _customerSettings;
|
|
|
|
protected readonly IWebHelper _webHelper;
|
|
|
|
public NopLogWriter(INopLoggerMsSqlNopDataProvider nopLoggerMsSqlNopDataProvider, CommonSettings commonSettings, CustomerSettings customerSettings, IWebHelper webHelper,
|
|
global::Nop.Services.Logging.ILogger nopLogger) : this(nopLoggerMsSqlNopDataProvider, commonSettings, customerSettings, webHelper,nopLogger, null)
|
|
{ }
|
|
|
|
public NopLogWriter(INopLoggerMsSqlNopDataProvider nopLoggerMsSqlNopDataProvider,
|
|
CommonSettings commonSettings, CustomerSettings customerSettings, IWebHelper webHelper, global::Nop.Services.Logging.ILogger nopLogger, string? categoryName = null) : base(categoryName)
|
|
{
|
|
_nopLogger = nopLogger;
|
|
_dataProvider = (NopLoggerMsSqlNopDataProvider)nopLoggerMsSqlNopDataProvider;
|
|
|
|
_commonSettings = commonSettings;
|
|
_customerSettings = customerSettings;
|
|
_webHelper = webHelper;
|
|
}
|
|
|
|
//public NopLogWriter(ILogger nopLogger, AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
|
|
//{
|
|
// _nopLogger=nopLogger;
|
|
//}
|
|
|
|
protected override void WriteLogItemCallback(AcLogItem logItem)
|
|
{
|
|
//using (_lockSlim.UseWait())
|
|
{
|
|
switch (logItem.LogLevel)
|
|
{
|
|
case LogLevel.Detail:
|
|
case LogLevel.Trace:
|
|
case LogLevel.Debug:
|
|
//if (_nopLogger.IsEnabled(LogLevelNop.Debug)) InsertLog(LogLevelNop.Debug, logItem.Text, logItem.Exception, null);
|
|
//break;
|
|
case LogLevel.Info:
|
|
if (_nopLogger.IsEnabled(LogLevelNop.Information)) InsertLog(LogLevelNop.Information, logItem.Text, logItem.Exception, null);
|
|
//_nopLogger.Information(logItem.Text); //.Forget();
|
|
break;
|
|
case LogLevel.Suggest:
|
|
case LogLevel.Warning:
|
|
if (_nopLogger.IsEnabled(LogLevelNop.Warning)) InsertLog(LogLevelNop.Warning, logItem.Text, logItem.Exception, null);
|
|
//_nopLogger.Warning(logItem.Text); //.Forget();
|
|
break;
|
|
case LogLevel.Error:
|
|
if (_nopLogger.IsEnabled(LogLevelNop.Error)) InsertLog(LogLevelNop.Error, logItem.Text, logItem.Exception, null);
|
|
//_nopLogger.Error(logItem.Text); //.Forget();//, logItem.Exception);
|
|
break;
|
|
case LogLevel.Disabled:
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void InsertLog(LogLevelNop logLevel, string shortMessage, string fullMessage = "", Customer customer = null)
|
|
{
|
|
//check ignore word/phrase list?
|
|
//if (IgnoreLog(shortMessage) || IgnoreLog(fullMessage))
|
|
// return;
|
|
|
|
_dataProvider.InsertLogItem(PrepareLog(logLevel, shortMessage, fullMessage, customer));
|
|
//_dataProvider.InsertLogItemAsync(PrepareLog(logLevel, shortMessage, fullMessage, customer)).Forget();
|
|
}
|
|
|
|
private Log PrepareLog(LogLevelNop 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
|
|
};
|
|
}
|
|
}
|
|
}
|