DocSpace-client/common/ASC.Common/Logging/EFLoggerFactory.cs

120 lines
3.9 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
using ILogger = Microsoft.Extensions.Logging.ILogger;
2022-01-26 10:42:30 +00:00
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
2019-11-26 09:31:53 +00:00
namespace ASC.Common.Logging;
[Singletone]
public class EFLoggerFactory : ILoggerFactory
2019-11-26 09:31:53 +00:00
{
private readonly Lazy<ILogger> _logger;
private readonly ILoggerProvider _loggerProvider;
2019-11-26 09:31:53 +00:00
public EFLoggerFactory(EFLoggerProvider loggerProvider)
{
_loggerProvider = loggerProvider;
_logger = new Lazy<ILogger>(() => _loggerProvider.CreateLogger(""));
}
2019-11-26 09:31:53 +00:00
public void AddProvider(ILoggerProvider provider)
{
//LoggerProvider = provider;
}
2019-11-26 09:31:53 +00:00
2022-02-08 11:07:28 +00:00
public ILogger CreateLogger(string categoryName)
{
return _logger.Value;
}
2019-11-26 09:31:53 +00:00
public void Dispose() { }
}
2019-11-26 09:31:53 +00:00
[Singletone]
public class EFLoggerProvider : ILoggerProvider
{
private readonly IOptionsMonitor<ILog> _option;
2019-11-26 09:31:53 +00:00
2022-02-08 11:07:28 +00:00
public EFLoggerProvider(IOptionsMonitor<ILog> option)
{
_option = option;
}
2019-11-26 09:31:53 +00:00
2022-02-08 11:07:28 +00:00
public ILogger CreateLogger(string categoryName)
{
return new EFLogger(_option.Get("ASC.SQL"));
}
2019-11-26 09:31:53 +00:00
public void Dispose() { }
}
2019-11-26 09:31:53 +00:00
public class EFLogger : ILogger
{
public ILog CustomLogger { get; }
2019-11-26 09:31:53 +00:00
2022-02-08 11:07:28 +00:00
public EFLogger(ILog customLogger)
{
CustomLogger = customLogger;
}
2019-11-26 09:31:53 +00:00
2022-02-08 11:07:28 +00:00
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return logLevel switch
2019-11-26 09:31:53 +00:00
{
LogLevel.Trace => CustomLogger.IsTraceEnabled,
LogLevel.Information => CustomLogger.IsInfoEnabled,
LogLevel.None => false,
LogLevel.Debug => CustomLogger.IsDebugEnabled,
LogLevel.Warning => CustomLogger.IsWarnEnabled,
LogLevel.Error => CustomLogger.IsErrorEnabled,
LogLevel.Critical => CustomLogger.IsErrorEnabled,
2019-11-26 09:31:53 +00:00
_ => true,
};
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
switch (eventId.Id)
2019-11-26 09:31:53 +00:00
{
//case 20000:
// CustomLogger.Debug(formatter(state, exception));
// break;
case 20101:
var keyValuePairs = state as IEnumerable<KeyValuePair<string, object>>;
CustomLogger.DebugWithProps("", keyValuePairs);
break;
2019-11-26 09:31:53 +00:00
}
}
}