feat: integrate Serilog and Telegram notifications
TaxBaik CI/CD / build-and-deploy (push) Successful in 51s

- Add Serilog for structured logging (Console + File)
- Implement TelegramNotificationService for admin alerts
- Log successful/failed login attempts with Telegram notifications
- Add application startup/shutdown logging
- Log important events to Telegram Chat ID: -5585148480
- Configuration: Telegram:BotToken and Telegram:ChatId in appsettings

Features:
- Automatic daily log rotation
- Structured logging with timestamps
- Environment-aware alerts
- Error and info level Telegram messages

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-06-28 16:19:38 +09:00
parent e797da6140
commit 2bde490e9e
15 changed files with 517 additions and 8 deletions
@@ -1,5 +1,6 @@
namespace TaxBaik.Web.Services;
using System.Net.Http;
using System.Net.Http.Json;
using TaxBaik.Application.DTOs;
using TaxBaik.Domain.Entities;
@@ -17,17 +18,28 @@ public class AnnouncementBrowserClient : IAnnouncementBrowserClient
{
private readonly HttpClient _http;
private readonly ILogger<AnnouncementBrowserClient> _logger;
private readonly ITokenStore _tokenStore;
public AnnouncementBrowserClient(HttpClient http, ILogger<AnnouncementBrowserClient> logger)
public AnnouncementBrowserClient(HttpClient http, ILogger<AnnouncementBrowserClient> logger, ITokenStore tokenStore)
{
_http = http;
_logger = logger;
_tokenStore = tokenStore;
}
private void EnsureAuthHeader()
{
if (!string.IsNullOrEmpty(_tokenStore.AccessToken) && !_http.DefaultRequestHeaders.Contains("Authorization"))
{
_http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
}
}
public async Task<IEnumerable<Announcement>> GetAllAsync(CancellationToken ct = default)
{
try
{
EnsureAuthHeader();
var result = await _http.GetFromJsonAsync<AnnouncementListResponse>("announcement", cancellationToken: ct);
return result?.Data ?? [];
}
@@ -42,6 +54,7 @@ public class AnnouncementBrowserClient : IAnnouncementBrowserClient
{
try
{
EnsureAuthHeader();
return await _http.GetFromJsonAsync<Announcement>($"announcement/{id}", cancellationToken: ct);
}
catch (HttpRequestException ex)
@@ -55,6 +68,7 @@ public class AnnouncementBrowserClient : IAnnouncementBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PostAsJsonAsync("announcement", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode) return null;
@@ -74,6 +88,7 @@ public class AnnouncementBrowserClient : IAnnouncementBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PutAsJsonAsync($"announcement/{id}", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode) return null;
@@ -93,6 +108,7 @@ public class AnnouncementBrowserClient : IAnnouncementBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.DeleteAsync($"announcement/{id}", cancellationToken: ct);
return response.IsSuccessStatusCode;
}
+13 -1
View File
@@ -12,15 +12,21 @@ public class AuthService
{
private readonly IAdminUserRepository _adminUserRepository;
private readonly ILogger<AuthService> _logger;
private readonly ITelegramNotificationService _telegramService;
private readonly string _jwtSecretKey;
private readonly string? _passwordResetToken;
private readonly int _accessTokenExpirationMinutes = 60; // Access Token: 1시간 (사용성 향상)
private readonly int _refreshTokenExpirationMinutes = 10080; // Refresh Token: 7일
public AuthService(IAdminUserRepository adminUserRepository, ILogger<AuthService> logger, IConfiguration configuration)
public AuthService(
IAdminUserRepository adminUserRepository,
ILogger<AuthService> logger,
IConfiguration configuration,
ITelegramNotificationService telegramService)
{
_adminUserRepository = adminUserRepository;
_logger = logger;
_telegramService = telegramService;
_jwtSecretKey = configuration["Jwt:SecretKey"] ?? throw new InvalidOperationException("Missing 'Jwt:SecretKey' configuration.");
_passwordResetToken = configuration["Admin:PasswordResetToken"];
}
@@ -46,10 +52,16 @@ public class AuthService
if (!BCrypt.Net.BCrypt.Verify(password, user.PasswordHash))
{
_logger.LogWarning("로그인 시도: 잘못된 비밀번호 '{Username}'", username);
await _telegramService.SendErrorAsync(
"로그인 실패",
$"사용자: {username}\n실패 사유: 잘못된 비밀번호");
return null;
}
_logger.LogInformation("로그인 성공: {Username}", username);
await _telegramService.SendInfoAsync(
"관리자 로그인",
$"사용자: {username}");
await _adminUserRepository.UpdateLastLoginAtAsync(user.Id);
return GenerateTokenPair(user);
}
+17 -1
View File
@@ -1,5 +1,6 @@
namespace TaxBaik.Web.Services;
using System.Net.Http;
using System.Net.Http.Json;
using TaxBaik.Application.DTOs;
using TaxBaik.Domain.Entities;
@@ -22,11 +23,21 @@ public class ClientBrowserClient : IClientBrowserClient
{
private readonly HttpClient _http;
private readonly ILogger<ClientBrowserClient> _logger;
private readonly ITokenStore _tokenStore;
public ClientBrowserClient(HttpClient http, ILogger<ClientBrowserClient> logger)
public ClientBrowserClient(HttpClient http, ILogger<ClientBrowserClient> logger, ITokenStore tokenStore)
{
_http = http;
_logger = logger;
_tokenStore = tokenStore;
}
private void EnsureAuthHeader()
{
if (!string.IsNullOrEmpty(_tokenStore.AccessToken) && !_http.DefaultRequestHeaders.Contains("Authorization"))
{
_http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
}
}
public async Task<(IEnumerable<Client> Items, int Total)> GetPagedAsync(
@@ -34,6 +45,7 @@ public class ClientBrowserClient : IClientBrowserClient
{
try
{
EnsureAuthHeader();
var query = $"client?page={page}&pageSize={pageSize}";
if (!string.IsNullOrEmpty(status))
query += $"&status={status}";
@@ -54,6 +66,7 @@ public class ClientBrowserClient : IClientBrowserClient
{
try
{
EnsureAuthHeader();
return await _http.GetFromJsonAsync<Client>($"client/{id}", cancellationToken: ct);
}
catch (HttpRequestException ex)
@@ -67,6 +80,7 @@ public class ClientBrowserClient : IClientBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PostAsJsonAsync("client", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
@@ -87,6 +101,7 @@ public class ClientBrowserClient : IClientBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PutAsJsonAsync($"client/{id}", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
@@ -107,6 +122,7 @@ public class ClientBrowserClient : IClientBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.DeleteAsync($"client/{id}", cancellationToken: ct);
return response.IsSuccessStatusCode;
}
+17 -1
View File
@@ -1,5 +1,6 @@
namespace TaxBaik.Web.Services;
using System.Net.Http;
using System.Net.Http.Json;
using TaxBaik.Domain.Entities;
@@ -16,17 +17,28 @@ public class FaqBrowserClient : IFaqBrowserClient
{
private readonly HttpClient _http;
private readonly ILogger<FaqBrowserClient> _logger;
private readonly ITokenStore _tokenStore;
public FaqBrowserClient(HttpClient http, ILogger<FaqBrowserClient> logger)
public FaqBrowserClient(HttpClient http, ILogger<FaqBrowserClient> logger, ITokenStore tokenStore)
{
_http = http;
_logger = logger;
_tokenStore = tokenStore;
}
private void EnsureAuthHeader()
{
if (!string.IsNullOrEmpty(_tokenStore.AccessToken) && !_http.DefaultRequestHeaders.Contains("Authorization"))
{
_http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
}
}
public async Task<IEnumerable<Faq>> GetAllAsync(CancellationToken ct = default)
{
try
{
EnsureAuthHeader();
var result = await _http.GetFromJsonAsync<FaqListResponse>("faq", cancellationToken: ct);
return result?.Data ?? [];
}
@@ -41,6 +53,7 @@ public class FaqBrowserClient : IFaqBrowserClient
{
try
{
EnsureAuthHeader();
return await _http.GetFromJsonAsync<Faq>($"faq/{id}", cancellationToken: ct);
}
catch (HttpRequestException ex)
@@ -54,6 +67,7 @@ public class FaqBrowserClient : IFaqBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PostAsJsonAsync("faq", faq, cancellationToken: ct);
if (!response.IsSuccessStatusCode) return null;
@@ -73,6 +87,7 @@ public class FaqBrowserClient : IFaqBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PutAsJsonAsync($"faq/{id}", faq, cancellationToken: ct);
if (!response.IsSuccessStatusCode) return null;
@@ -92,6 +107,7 @@ public class FaqBrowserClient : IFaqBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.DeleteAsync($"faq/{id}", cancellationToken: ct);
return response.IsSuccessStatusCode;
}
+18 -1
View File
@@ -1,5 +1,6 @@
namespace TaxBaik.Web.Services;
using System.Net.Http;
using System.Net.Http.Json;
using TaxBaik.Domain.Entities;
@@ -20,17 +21,28 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
private readonly HttpClient _http;
private readonly ILogger<TaxFilingBrowserClient> _logger;
private readonly ITokenStore _tokenStore;
public TaxFilingBrowserClient(HttpClient http, ILogger<TaxFilingBrowserClient> logger)
public TaxFilingBrowserClient(HttpClient http, ILogger<TaxFilingBrowserClient> logger, ITokenStore tokenStore)
{
_http = http;
_logger = logger;
_tokenStore = tokenStore;
}
private void EnsureAuthHeader()
{
if (!string.IsNullOrEmpty(_tokenStore.AccessToken) && !_http.DefaultRequestHeaders.Contains("Authorization"))
{
_http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
}
}
public async Task<IEnumerable<TaxFiling>> GetUpcomingAsync(int daysAhead = 30, CancellationToken ct = default)
{
try
{
EnsureAuthHeader();
var result = await _http.GetFromJsonAsync<TaxFilingListResponse>(
$"tax-filing/upcoming?daysAhead={daysAhead}", cancellationToken: ct);
return result?.Data ?? [];
@@ -46,6 +58,7 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
try
{
EnsureAuthHeader();
var result = await _http.GetFromJsonAsync<TaxFilingListResponse>(
$"tax-filing/client/{clientId}", cancellationToken: ct);
return result?.Data ?? [];
@@ -61,6 +74,7 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
try
{
EnsureAuthHeader();
return await _http.GetFromJsonAsync<TaxFiling>(
$"tax-filing/{id}", cancellationToken: ct);
}
@@ -75,6 +89,7 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PostAsJsonAsync("tax-filing", filing, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
@@ -95,6 +110,7 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.PutAsJsonAsync($"tax-filing/{id}", filing, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
@@ -115,6 +131,7 @@ public class TaxFilingBrowserClient : ITaxFilingBrowserClient
{
try
{
EnsureAuthHeader();
var response = await _http.DeleteAsync($"tax-filing/{id}", cancellationToken: ct);
return response.IsSuccessStatusCode;
}
@@ -0,0 +1,76 @@
namespace TaxBaik.Web.Services;
using System.Net.Http.Json;
/// <summary>
/// Telegram Bot 알림 서비스
/// 중요 로깅 및 오류를 Telegram으로 전송
/// </summary>
public interface ITelegramNotificationService
{
Task SendMessageAsync(string message, CancellationToken ct = default);
Task SendErrorAsync(string title, string details, CancellationToken ct = default);
Task SendInfoAsync(string title, string message, CancellationToken ct = default);
}
public class TelegramNotificationService : ITelegramNotificationService
{
private readonly HttpClient _httpClient;
private readonly ILogger<TelegramNotificationService> _logger;
private readonly string _botToken;
private readonly string _chatId;
private const string TelegramApiUrl = "https://api.telegram.org";
public TelegramNotificationService(
HttpClient httpClient,
ILogger<TelegramNotificationService> logger,
IConfiguration config)
{
_httpClient = httpClient;
_logger = logger;
_botToken = config["Telegram:BotToken"] ?? "";
_chatId = config["Telegram:ChatId"] ?? "";
}
public async Task SendMessageAsync(string message, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(_botToken) || string.IsNullOrEmpty(_chatId))
{
_logger.LogWarning("Telegram credentials not configured");
return;
}
try
{
var url = $"{TelegramApiUrl}/bot{_botToken}/sendMessage";
var payload = new
{
chat_id = _chatId,
text = message,
parse_mode = "HTML"
};
var response = await _httpClient.PostAsJsonAsync(url, payload, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Failed to send Telegram message: {StatusCode}", response.StatusCode);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending Telegram message");
}
}
public async Task SendErrorAsync(string title, string details, CancellationToken ct = default)
{
var message = $"<b>❌ {title}</b>\n\n{details}\n\n<i>{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</i>";
await SendMessageAsync(message, ct);
}
public async Task SendInfoAsync(string title, string message, CancellationToken ct = default)
{
var text = $"<b>️ {title}</b>\n\n{message}\n\n<i>{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</i>";
await SendMessageAsync(text, ct);
}
}