perf: 优化 log 的使用方式,通过 autofac + NullLogger 配合使用。

main
NoahLan 6 months ago
parent 9b5f19c02f
commit 8790954b9a

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using NPin.Framework.SettingManagement.Domain;
using NPin.Framework.Upms.Domain.Settings;
@ -12,25 +13,25 @@ namespace NPin.Framework.Upms.Domain.Managers;
public class SmsManager : DomainService
{
private readonly ILogger<SmsManager> _logger;
public ILogger<SmsManager> Logger { get; set; }
private readonly ISettingManager _settingManager;
private readonly IEnumerable<ISms> _smsProviders;
public SmsManager(ILogger<SmsManager> logger, ISettingManager settingManager, IEnumerable<ISms> smsProviders)
public SmsManager(ISettingManager settingManager, IEnumerable<ISms> smsProviders)
{
_logger = logger;
Logger = NullLogger<SmsManager>.Instance;
_settingManager = settingManager;
_smsProviders = smsProviders;
}
public async Task<SmsConfigModel> GetSmsConfigModelAsync()
{
var configJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsConfigModelKey);
var settingsJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsSettingsKey);
var providersJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsProvidersKey);
var templateJson = await _settingManager.GetOrNullUpmsAsync(ConfigConst.SmsTemplatesKey);
var configJson = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SmsConfigModel);
var settingsJson = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SmsSettings);
var providersJson = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SmsProviders);
var templateJson = await _settingManager.GetOrNullUpmsAsync(ConfigKeyConst.SmsTemplates);
var ret = JsonConvert.DeserializeObject<SmsConfigModel>(configJson ?? string.Empty) ?? new SmsConfigModel();
@ -71,7 +72,7 @@ public class SmsManager : DomainService
}
catch (Exception ex)
{
_logger.LogError(ex, $"短信发送失败: {ex.Message}");
Logger.LogError(ex, $"短信发送失败: {ex.Message}");
throw new UserFriendlyException($"短信发送失败: {ex.Message}");
}
}

@ -2,6 +2,7 @@
using AlibabaCloud.SDK.Dysmsapi20170525;
using AlibabaCloud.SDK.Dysmsapi20170525.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using NPin.Framework.Upms.Domain.Shared.Enums;
using NPin.Framework.Upms.Domain.Shared.Model;
@ -11,11 +12,11 @@ namespace NPin.Framework.Upms.Domain.Sms.Handler;
public class AliyunSmsHandler : ISms, ISingletonDependency
{
private readonly ILogger<AliyunSmsHandler> _logger;
public ILogger<AliyunSmsHandler> Logger { get; set; }
public AliyunSmsHandler(ILogger<AliyunSmsHandler> logger)
public AliyunSmsHandler()
{
_logger = logger;
Logger = NullLogger<AliyunSmsHandler>.Instance;
}
public SmsProviderTypeEnum ProviderType => SmsProviderTypeEnum.Aliyun;
@ -37,7 +38,7 @@ public class AliyunSmsHandler : ISms, ISingletonDependency
var response = await client.SendSmsAsync(sendSmsRequest);
// TODO 判断结果
_logger.LogDebug(response.ToString());
Logger.LogDebug(response.ToString());
}
private Client CreateClient(SmsProvider provider)

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NPin.Framework.Upms.Domain.Shared.Enums;
using NPin.Framework.Upms.Domain.Shared.Model;
using TencentCloud.Common;
@ -11,11 +12,11 @@ namespace NPin.Framework.Upms.Domain.Sms.Handler;
public class TencentSmsHandler : ISms, ISingletonDependency
{
private readonly ILogger<AliyunSmsHandler> _logger;
public ILogger<TencentSmsHandler> Logger { get; set; }
public TencentSmsHandler(ILogger<AliyunSmsHandler> logger)
public TencentSmsHandler()
{
_logger = logger;
Logger = NullLogger<TencentSmsHandler>.Instance;
}
public SmsProviderTypeEnum ProviderType => SmsProviderTypeEnum.Tencent;
@ -37,7 +38,7 @@ public class TencentSmsHandler : ISms, ISingletonDependency
var response = await client.SendSms(sendSmsRequest);
// TODO 判断结果
_logger.LogDebug(response.ToString());
Logger.LogDebug(response.ToString());
}
private SmsClient CreateClient(SmsProvider provider)

Loading…
Cancel
Save