mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-06-08 08:59:36 +08:00
feat(logManagement): implement log management service
This commit is contained in:
@@ -6,17 +6,20 @@ using COSXML.Model.Tag;
|
||||
using COSXML.Transfer;
|
||||
using Foxel.Services.Attributes;
|
||||
using Foxel.Services.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Services.Storage.Providers;
|
||||
|
||||
public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProvider
|
||||
{
|
||||
private readonly IConfigService _configService;
|
||||
private readonly ILogger<CustomQCloudCredentialProvider> _logger;
|
||||
|
||||
public CustomQCloudCredentialProvider(IConfigService configService)
|
||||
public CustomQCloudCredentialProvider(IConfigService configService, ILogger<CustomQCloudCredentialProvider> logger)
|
||||
: base(null, null, 0L, null)
|
||||
{
|
||||
_configService = configService;
|
||||
_logger = logger;
|
||||
Refresh();
|
||||
}
|
||||
|
||||
@@ -34,14 +37,14 @@ public class CustomQCloudCredentialProvider : DefaultSessionQCloudCredentialProv
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"刷新临时密钥时出错: {ex.Message}");
|
||||
_logger.LogError(ex, "刷新临时密钥时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[StorageProvider(StorageType.Cos)]
|
||||
public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
public class CosStorageProvider(IConfigService configService, ILogger<CosStorageProvider> logger) : IStorageProvider
|
||||
{
|
||||
private CosXml CreateClient()
|
||||
{
|
||||
@@ -51,7 +54,10 @@ public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
.SetDebugLog(true)
|
||||
.Build();
|
||||
|
||||
var cosCredentialProvider = new CustomQCloudCredentialProvider(configService);
|
||||
var cosCredentialProvider = new CustomQCloudCredentialProvider(configService,
|
||||
logger.IsEnabled(LogLevel.Debug) ?
|
||||
Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<CustomQCloudCredentialProvider>() :
|
||||
Microsoft.Extensions.Logging.Abstractions.NullLogger<CustomQCloudCredentialProvider>.Instance);
|
||||
|
||||
return new CosXmlServer(config, cosCredentialProvider);
|
||||
}
|
||||
@@ -93,17 +99,17 @@ public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (CosClientException clientEx)
|
||||
{
|
||||
Console.WriteLine($"COS客户端异常: {clientEx}");
|
||||
logger.LogError(clientEx, "COS客户端异常");
|
||||
throw;
|
||||
}
|
||||
catch (CosServerException serverEx)
|
||||
{
|
||||
Console.WriteLine($"COS服务器异常: {serverEx.GetInfo()}");
|
||||
logger.LogError(serverEx, "COS服务器异常: {ServerInfo}", serverEx.GetInfo());
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"上传文件到腾讯云COS时出错: {ex.Message}");
|
||||
logger.LogError(ex, "上传文件到腾讯云COS时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -121,15 +127,15 @@ public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (CosClientException clientEx)
|
||||
{
|
||||
Console.WriteLine($"COS客户端异常: {clientEx}");
|
||||
logger.LogWarning(clientEx, "COS客户端异常");
|
||||
}
|
||||
catch (CosServerException serverEx)
|
||||
{
|
||||
Console.WriteLine($"COS服务器异常: {serverEx.GetInfo()}");
|
||||
logger.LogWarning(serverEx, "COS服务器异常: {ServerInfo}", serverEx.GetInfo());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从腾讯云COS删除文件时出错: {ex.Message}");
|
||||
logger.LogWarning(ex, "从腾讯云COS删除文件时出错");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +177,7 @@ public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"生成腾讯云COS文件URL时出错: {ex.Message}");
|
||||
logger.LogError(ex, "生成腾讯云COS文件URL时出错");
|
||||
return "/images/unavailable.gif";
|
||||
}
|
||||
}
|
||||
@@ -204,17 +210,17 @@ public class CosStorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (CosClientException clientEx)
|
||||
{
|
||||
Console.WriteLine($"COS客户端异常: {clientEx}");
|
||||
logger.LogError(clientEx, "COS客户端异常");
|
||||
throw;
|
||||
}
|
||||
catch (CosServerException serverEx)
|
||||
{
|
||||
Console.WriteLine($"COS服务器异常: {serverEx.GetInfo()}");
|
||||
logger.LogError(serverEx, "COS服务器异常: {ServerInfo}", serverEx.GetInfo());
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从腾讯云COS下载文件时出错: {ex.Message}");
|
||||
logger.LogError(ex, "从腾讯云COS下载文件时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using Foxel.Services.Attributes;
|
||||
using Foxel.Services.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Services.Storage.Providers;
|
||||
|
||||
[StorageProvider(StorageType.Local)]
|
||||
public class LocalStorageProvider(IConfigService configService) : IStorageProvider
|
||||
public class LocalStorageProvider(IConfigService configService, ILogger<LocalStorageProvider> logger) : IStorageProvider
|
||||
{
|
||||
private readonly string _baseDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Uploads");
|
||||
|
||||
|
||||
@@ -3,11 +3,12 @@ using Amazon.S3.Model;
|
||||
using Amazon.S3.Transfer;
|
||||
using Foxel.Services.Attributes;
|
||||
using Foxel.Services.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Services.Storage.Providers;
|
||||
|
||||
[StorageProvider(StorageType.S3)]
|
||||
public class S3StorageProvider(IConfigService configService) : IStorageProvider
|
||||
public class S3StorageProvider(IConfigService configService, ILogger<S3StorageProvider> logger) : IStorageProvider
|
||||
{
|
||||
private AmazonS3Client CreateClient()
|
||||
{
|
||||
@@ -63,7 +64,7 @@ public class S3StorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"上传文件到S3时出错: {ex.Message}");
|
||||
logger.LogError(ex, "上传文件到S3时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -86,7 +87,7 @@ public class S3StorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从S3删除文件时出错: {ex.Message}");
|
||||
logger.LogWarning(ex, "从S3删除文件时出错");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +119,7 @@ public class S3StorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"生成S3文件URL时出错: {ex.Message}");
|
||||
logger.LogError(ex, "生成S3文件URL时出错");
|
||||
return "/images/unavailable.gif";
|
||||
}
|
||||
}
|
||||
@@ -159,7 +160,7 @@ public class S3StorageProvider(IConfigService configService) : IStorageProvider
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从S3下载文件时出错: {ex.Message}");
|
||||
logger.LogError(ex, "从S3下载文件时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ using System.Text.Json.Serialization;
|
||||
using Foxel.Services.Attributes;
|
||||
using Foxel.Services.Configuration;
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Services.Storage.Providers;
|
||||
|
||||
[StorageProvider(StorageType.Telegram)]
|
||||
public class TelegramStorageProvider(IConfigService configService) : IStorageProvider
|
||||
public class TelegramStorageProvider(IConfigService configService, ILogger<TelegramStorageProvider> logger) : IStorageProvider
|
||||
{
|
||||
public async Task<string> SaveAsync(Stream fileStream, string fileName, string contentType)
|
||||
{
|
||||
@@ -40,7 +41,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
Console.WriteLine($"Telegram API 请求失败: 状态码: {response.StatusCode}, 响应: {errorContent}");
|
||||
logger.LogError("Telegram API 请求失败: 状态码: {StatusCode}, 响应: {Response}", response.StatusCode, errorContent);
|
||||
throw new ApplicationException($"Telegram API 请求失败: {response.StatusCode}");
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"发送文件到 Telegram 时出错: {ex.Message}");
|
||||
logger.LogError(ex, "发送文件到 Telegram 时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -114,7 +115,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"删除 Telegram 文件时出错: {ex.Message}");
|
||||
logger.LogWarning(ex, "删除 Telegram 文件时出错");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +134,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"生成 Telegram 文件 URL 时出错: {ex.Message}");
|
||||
logger.LogError(ex, "生成 Telegram 文件 URL 时出错");
|
||||
return $"/images/unavailable.gif";
|
||||
}
|
||||
}
|
||||
@@ -203,7 +204,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"下载 Telegram 文件时出错: {ex.Message}");
|
||||
logger.LogError(ex, "下载 Telegram 文件时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using Foxel.Services.Attributes;
|
||||
using Foxel.Services.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Foxel.Services.Storage.Providers;
|
||||
|
||||
[StorageProvider(StorageType.WebDAV)]
|
||||
public class WebDavStorageProvider(IConfigService configService) : IStorageProvider
|
||||
public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavStorageProvider> logger) : IStorageProvider
|
||||
{
|
||||
private HttpClient CreateClient()
|
||||
{
|
||||
@@ -55,7 +56,7 @@ public class WebDavStorageProvider(IConfigService configService) : IStorageProvi
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"上传文件到WebDAV时出错: {ex.Message}");
|
||||
logger.LogError(ex, "上传文件到WebDAV时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +81,7 @@ public class WebDavStorageProvider(IConfigService configService) : IStorageProvi
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从WebDAV删除文件时出错: {ex.Message}");
|
||||
logger.LogWarning(ex, "从WebDAV删除文件时出错");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +104,7 @@ public class WebDavStorageProvider(IConfigService configService) : IStorageProvi
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"生成WebDAV文件URL时出错: {ex.Message}");
|
||||
logger.LogError(ex, "生成WebDAV文件URL时出错");
|
||||
return "/images/unavailable.gif";
|
||||
}
|
||||
}
|
||||
@@ -144,7 +145,7 @@ public class WebDavStorageProvider(IConfigService configService) : IStorageProvi
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"从WebDAV下载文件时出错: {ex.Message}");
|
||||
logger.LogError(ex, "从WebDAV下载文件时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -210,7 +211,7 @@ public class WebDavStorageProvider(IConfigService configService) : IStorageProvi
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"确保WebDAV目录存在时出错: {ex.Message}");
|
||||
logger.LogError(ex, "确保WebDAV目录存在时出错");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user