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
+54 -1
View File
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.IdentityModel.Tokens;
using MudBlazor.Services;
using Serilog;
using TaxBaik.Application;
using TaxBaik.Application.Services;
using TaxBaik.Infrastructure;
@@ -16,6 +17,20 @@ using TaxBaik.Web.Services;
var builder = WebApplication.CreateBuilder(args);
var isProduction = builder.Environment.IsProduction();
// Serilog 설정
builder.Host.UseSerilog((context, config) =>
{
config
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File(
path: "logs/taxbaik-.log",
rollingInterval: RollingInterval.Day,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.Enrich.FromLogContext()
.Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName);
});
// Controllers (API)
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
@@ -72,6 +87,9 @@ builder.Services.AddAuthorizationCore();
// Notifications (SignalR)
builder.Services.AddScoped<INotificationService, NotificationService>();
// Telegram Notification
builder.Services.AddHttpClient<ITelegramNotificationService, TelegramNotificationService>();
// HTTP Client for API (with automatic token refresh)
builder.Services.AddScoped<ITokenStore, TokenStore>();
builder.Services.AddScoped<TokenRefreshHandler>();
@@ -202,4 +220,39 @@ app.MapRazorComponents<TaxBaik.Web.Components.Admin.App>()
.AddInteractiveServerRenderMode()
.AllowAnonymous();
app.Run();
// 애플리케이션 시작/종료 로깅
try
{
Log.Information("애플리케이션 시작: {Environment}", app.Environment.EnvironmentName);
if (!app.Environment.IsDevelopment())
{
using (var scope = app.Services.CreateScope())
{
var telegramService = scope.ServiceProvider.GetRequiredService<ITelegramNotificationService>();
await telegramService.SendInfoAsync(
"서버 시작",
$"환경: {app.Environment.EnvironmentName}");
}
}
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "애플리케이션 강종");
if (!app.Environment.IsDevelopment())
{
using (var scope = app.Services.CreateScope())
{
var telegramService = scope.ServiceProvider.GetRequiredService<ITelegramNotificationService>();
await telegramService.SendErrorAsync(
"서버 오류",
$"환경: {app.Environment.EnvironmentName}\n오류: {ex.Message}");
}
}
throw;
}
finally
{
Log.Information("애플리케이션 종료");
Log.CloseAndFlush();
}
@@ -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);
}
}
+3
View File
@@ -17,6 +17,9 @@
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.19.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.19.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.9" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
</Project>
+2 -2
View File
@@ -18,8 +18,8 @@
"BaseUrl": "http://localhost:5001/taxbaik/api/"
},
"Telegram": {
"BotToken": "",
"ChatId": ""
"BotToken": "8679990909:AAGLLRUIAuEbYAZVGOYDu-UuTu4ihroEiX0",
"ChatId": "-5585148480"
},
"Admin": {
"PasswordResetToken": "dev-reset-token-12345"