noplogger improvements, fixes, etc...

This commit is contained in:
Loretta 2025-10-21 15:30:43 +02:00
parent fd39b0700a
commit 38e036553e
2 changed files with 126 additions and 13 deletions

View File

@ -2,22 +2,94 @@
using AyCode.Entities; using AyCode.Entities;
using AyCode.Entities.Server.LogItems; using AyCode.Entities.Server.LogItems;
using AyCode.Utils.Extensions; using AyCode.Utils.Extensions;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.DataProvider;
using LinqToDB.DataProvider.SqlServer;
using Mango.Nop.Core.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 LogLevel = AyCode.Core.Loggers.LogLevel;
using LogLevelNop = Nop.Core.Domain.Logging.LogLevel; using LogLevelNop = Nop.Core.Domain.Logging.LogLevel;
namespace Mango.Nop.Core.Loggers namespace Mango.Nop.Core.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 public class NopLogWriter : AcLogItemWriterBase<AcLogItem>//AcTextLogWriterBase
{ {
private static readonly SemaphoreSlim LockSlim = new(0); private readonly SemaphoreSlim _lockSlim = new(0);
private NopLoggerMsSqlNopDataProvider _dataProvider;
private readonly global::Nop.Services.Logging.ILogger _nopLogger; private readonly global::Nop.Services.Logging.ILogger _nopLogger;
public NopLogWriter(global::Nop.Services.Logging.ILogger nopLogger) : this(nopLogger, null) 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(global::Nop.Services.Logging.ILogger nopLogger, string? categoryName = null) : base(categoryName) public NopLogWriter(INopLoggerMsSqlNopDataProvider nopLoggerMsSqlNopDataProvider,
CommonSettings commonSettings, CustomerSettings customerSettings, IWebHelper webHelper, global::Nop.Services.Logging.ILogger nopLogger, string? categoryName = null) : base(categoryName)
{ {
_nopLogger = nopLogger; _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) //public NopLogWriter(ILogger nopLogger, AppType appType, LogLevel logLevel, string? categoryName = null) : base(appType, logLevel, categoryName)
@ -27,26 +99,26 @@ namespace Mango.Nop.Core.Loggers
protected override void WriteLogItemCallback(AcLogItem logItem) protected override void WriteLogItemCallback(AcLogItem logItem)
{ {
using (LockSlim.UseWait()) //using (_lockSlim.UseWait())
{ {
switch (logItem.LogLevel) switch (logItem.LogLevel)
{ {
case LogLevel.Detail: case LogLevel.Detail:
case LogLevel.Trace: case LogLevel.Trace:
case LogLevel.Debug: case LogLevel.Debug:
if (_nopLogger.IsEnabled(LogLevelNop.Debug)) _nopLogger.InsertLog(LogLevelNop.Debug, logItem.Text, logItem.Exception, null); if (_nopLogger.IsEnabled(LogLevelNop.Debug)) InsertLog(LogLevelNop.Debug, logItem.Text, logItem.Exception, null);
break; break;
case LogLevel.Info: case LogLevel.Info:
if (_nopLogger.IsEnabled(LogLevelNop.Information)) _nopLogger.InsertLog(LogLevelNop.Information, logItem.Text, logItem.Exception, null); if (_nopLogger.IsEnabled(LogLevelNop.Information)) InsertLog(LogLevelNop.Information, logItem.Text, logItem.Exception, null);
//_nopLogger.Information(logItem.Text); //.Forget(); //_nopLogger.Information(logItem.Text); //.Forget();
break; break;
case LogLevel.Suggest: case LogLevel.Suggest:
case LogLevel.Warning: case LogLevel.Warning:
if (_nopLogger.IsEnabled(LogLevelNop.Warning)) _nopLogger.InsertLog(LogLevelNop.Warning, logItem.Text, logItem.Exception, null); if (_nopLogger.IsEnabled(LogLevelNop.Warning)) InsertLog(LogLevelNop.Warning, logItem.Text, logItem.Exception, null);
//_nopLogger.Warning(logItem.Text); //.Forget(); //_nopLogger.Warning(logItem.Text); //.Forget();
break; break;
case LogLevel.Error: case LogLevel.Error:
if (_nopLogger.IsEnabled(LogLevelNop.Error)) _nopLogger.InsertLog(LogLevelNop.Error, logItem.Text, logItem.Exception, null); if (_nopLogger.IsEnabled(LogLevelNop.Error)) InsertLog(LogLevelNop.Error, logItem.Text, logItem.Exception, null);
//_nopLogger.Error(logItem.Text); //.Forget();//, logItem.Exception); //_nopLogger.Error(logItem.Text); //.Forget();//, logItem.Exception);
break; break;
case LogLevel.Disabled: case LogLevel.Disabled:
@ -56,5 +128,30 @@ namespace Mango.Nop.Core.Loggers
} }
} }
} }
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
};
}
} }
} }

View File

@ -33,6 +33,20 @@ public class MgDbContextBase : IMgDbContextBase
DataProvider = dataProvider; DataProvider = dataProvider;
} }
private static TransactionScope CreateTransactionScope(TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required)
{
//TransactionManager.ImplicitDistributedTransactions = true;
var transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TransactionManager.DefaultTimeout
};
return new TransactionScope(transactionScopeOption, transactionOptions, TransactionScopeAsyncFlowOption.Enabled);
//return new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
}
public bool Transaction(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false) public bool Transaction(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false)
=> TransactionInner(callbackTransactionBody, throwException); => TransactionInner(callbackTransactionBody, throwException);
@ -76,7 +90,7 @@ public class MgDbContextBase : IMgDbContextBase
try try
{ {
using (var transaction = new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled)) using (var transaction = CreateTransactionScope())// new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled))
{ {
result = await callbackTransactionBody(transaction); result = await callbackTransactionBody(transaction);
@ -102,12 +116,14 @@ public class MgDbContextBase : IMgDbContextBase
try try
{ {
using var transaction = new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled); using(var transaction = CreateTransactionScope()) //new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled)
{
result = callbackTransactionBody(transaction);
result = callbackTransactionBody(transaction); if (result) transaction.Complete();
}
if (result) transaction.Complete(); if (!result) Logger.Warning($"TransactionInner({this}) transaction ROLLBACK!");
else Logger.Warning($"TransactionInner({this}) transaction ROLLBACK!");
} }
catch (Exception ex) catch (Exception ex)
{ {