Добавил логирование в телеграм

This commit is contained in:
2025-09-06 21:04:59 +03:00
parent 8fe37ff9d7
commit 5f52e16dbd
4 changed files with 35 additions and 10 deletions

View File

@@ -3,13 +3,17 @@ using Quartz;
using SfeduSchedule; using SfeduSchedule;
using SfeduSchedule.Jobs; using SfeduSchedule.Jobs;
using SfeduSchedule.Services; using SfeduSchedule.Services;
using X.Extensions.Logging.Telegram.Extensions;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration; var configuration = builder.Configuration;
string? preinstsalledJwtToken = configuration["TOKEN"]; string? preinstalledJwtToken = configuration["TOKEN"];
string? tgChatId = configuration["TG_CHAT_ID"];
string? tgToken = configuration["TG_TOKEN"];
string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 4 * ? * *"; string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 4 * ? * *";
// создать папку data если не существует // создать папку data если не существует
var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data"); var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data");
if (!Directory.Exists(dataDirectory)) if (!Directory.Exists(dataDirectory))
@@ -18,7 +22,23 @@ if (!Directory.Exists(dataDirectory))
} }
GlobalVariables.JwtFilePath = Path.Combine(dataDirectory, "jwt.txt"); GlobalVariables.JwtFilePath = Path.Combine(dataDirectory, "jwt.txt");
builder.Services.AddOpenApi();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
if (!string.IsNullOrEmpty(tgChatId) && !string.IsNullOrEmpty(tgToken))
builder.Logging.AddTelegram(options =>
{
options.ChatId = tgChatId;
options.AccessToken = tgToken;
options.FormatterConfiguration.UseEmoji = true;
options.FormatterConfiguration.ReadableApplicationName = "Sfedu Schedule";
options.LogLevel = new Dictionary<string, LogLevel>
{
{ "Default", LogLevel.Error },
{ "SfeduSchedule.Jobs.UpdateJwtJob", LogLevel.Information }
};
});
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddHttpClient<ModeusService>(); builder.Services.AddHttpClient<ModeusService>();
@@ -26,7 +46,7 @@ builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration)
var jobKey = new JobKey("UpdateJWTJob"); var jobKey = new JobKey("UpdateJWTJob");
if (string.IsNullOrEmpty(preinstsalledJwtToken)) if (string.IsNullOrEmpty(preinstalledJwtToken))
{ {
builder.Services.AddQuartz(q => builder.Services.AddQuartz(q =>
{ {
@@ -56,7 +76,7 @@ app.UseForwardedHeaders();
var logger = app.Services.GetRequiredService<ILogger<Program>>(); var logger = app.Services.GetRequiredService<ILogger<Program>>();
if (string.IsNullOrEmpty(preinstsalledJwtToken)) if (string.IsNullOrEmpty(preinstalledJwtToken))
{ {
var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>(); var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
var scheduler = await schedulerFactory.GetScheduler(); var scheduler = await schedulerFactory.GetScheduler();
@@ -106,7 +126,7 @@ app.UseStaticFiles();
app.MapGet("/", async context => app.MapGet("/", async context =>
{ {
context.Response.ContentType = "text/html; charset=utf-8"; context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync(Path.Combine(app.Environment.WebRootPath ?? "wwwroot", "index.html")); await context.Response.SendFileAsync(Path.Combine(app.Environment.WebRootPath, "index.html"));
}); });
app.MapControllers(); app.MapControllers();

View File

@@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Playwright" Version="1.55.0" /> <PackageReference Include="Microsoft.Playwright" Version="1.55.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.15.0" /> <PackageReference Include="Quartz.AspNetCore" Version="3.15.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
<PackageReference Include="X.Extensions.Logging.Telegram" Version="2.0.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -12,7 +12,9 @@ services:
- AzureAd:CallbackPath=/signin-oidc - AzureAd:CallbackPath=/signin-oidc
- MS_USERNAME=${MS_USERNAME} - MS_USERNAME=${MS_USERNAME}
- MS_PASSWORD=${MS_PASSWORD} - MS_PASSWORD=${MS_PASSWORD}
# - TOKEN= - TG_CHAT_ID=${TG_CHAT_ID}
- TG_TOKEN=${TG_TOKEN}
# - TOKEN=${TOKEN}
volumes: volumes:
- data:/app/data - data:/app/data
restart: always restart: always

View File

@@ -10,9 +10,11 @@ services:
- AzureAd:ClientSecret= - AzureAd:ClientSecret=
- AzureAd:Domain=sfedu.onmicrosoft.com - AzureAd:Domain=sfedu.onmicrosoft.com
- AzureAd:CallbackPath=/signin-oidc - AzureAd:CallbackPath=/signin-oidc
- MS_USERNAME= - MS_USERNAME=${MS_USERNAME}
- MS_PASSWORD= - MS_PASSWORD=${MS_PASSWORD}
# - TOKEN= - TG_CHAT_ID=${TG_CHAT_ID}
- TG_TOKEN=${TG_TOKEN}
# - TOKEN=${TOKEN}
volumes: volumes:
- ./data:/app/data - ./data:/app/data
restart: unless-stopped restart: unless-stopped