diff --git a/.gitignore b/.gitignore index 0947b55..f80b957 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ app.db app.db-shm app.db-wal .mono +logs/ # Node.js dependencies node_modules/ diff --git a/src/RazorPagesProject/Data/ApplicationDbContext.cs b/src/RazorPagesProject/Data/ApplicationDbContext.cs index 8d05fee..e4c2442 100644 --- a/src/RazorPagesProject/Data/ApplicationDbContext.cs +++ b/src/RazorPagesProject/Data/ApplicationDbContext.cs @@ -28,12 +28,7 @@ public async virtual Task AddMessageAsync(Message message) public async virtual Task DeleteAllMessagesAsync() { - foreach (var message in Messages) - { - Messages.Remove(message); - } - - await SaveChangesAsync(); + await Messages.ExecuteDeleteAsync(); } public async virtual Task DeleteMessageAsync(int id) diff --git a/src/RazorPagesProject/Pages/GitHubProfile.cshtml.cs b/src/RazorPagesProject/Pages/GitHubProfile.cshtml.cs index f6b1f6a..cebbeb0 100644 --- a/src/RazorPagesProject/Pages/GitHubProfile.cshtml.cs +++ b/src/RazorPagesProject/Pages/GitHubProfile.cshtml.cs @@ -6,9 +6,10 @@ namespace RazorPagesProject.Pages; -public class GitHubProfileModel(IGitHubClient client, IStringLocalizer localizer) : PageModel +public class GitHubProfileModel(IGitHubClient client, IStringLocalizer localizer, ILogger logger) : PageModel { private readonly IStringLocalizer _localizer = localizer; + private readonly ILogger _logger = logger; public class InputModel { @@ -29,7 +30,24 @@ public async Task OnGetAsync([FromRoute] string userName) { if (userName != null) { - GitHubUser = await Client.GetUserAsync(userName); + try + { + GitHubUser = await Client.GetUserAsync(userName); + } + catch (HttpRequestException ex) + { + _logger.LogError(ex, "Failed to fetch GitHub profile for user '{UserName}'. Status: {StatusCode}", + userName, ex.StatusCode); + ModelState.AddModelError(string.Empty, _localizer["ErrorFetchingProfile"]); + return Page(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Unexpected error occurred while fetching GitHub profile for user '{UserName}'", + userName); + ModelState.AddModelError(string.Empty, _localizer["UnexpectedError"]); + return Page(); + } } return Page(); diff --git a/src/RazorPagesProject/Program.cs b/src/RazorPagesProject/Program.cs index 46e5e66..a8c3138 100644 --- a/src/RazorPagesProject/Program.cs +++ b/src/RazorPagesProject/Program.cs @@ -5,9 +5,18 @@ using RazorPagesProject.Data; using RazorPagesProject.Services; using System.Globalization; +using Serilog; var builder = WebApplication.CreateBuilder(args); +builder.Host.UseSerilog((context, services, configuration) => configuration + .ReadFrom.Configuration(context.Configuration) + .ReadFrom.Services(services) + .Enrich.FromLogContext() + .WriteTo.Console() + .WriteTo.File("logs/app-{Date}.txt", rollingInterval: RollingInterval.Day)); + + // Add services to the container. var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); @@ -44,7 +53,8 @@ { client.BaseAddress = new Uri("https://api.github.com"); client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Yolo", "0.1.0")); -}); +}) +.AddStandardResilienceHandler(); builder.Services.AddScoped(); @@ -60,11 +70,11 @@ else { app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); - app.UseHttpsRedirection(); } +app.UseHttpsRedirection(); + app.UseStaticFiles(); app.UseRequestLocalization(); diff --git a/src/RazorPagesProject/RazorPagesProject.csproj b/src/RazorPagesProject/RazorPagesProject.csproj index 5735db2..71a1c83 100644 --- a/src/RazorPagesProject/RazorPagesProject.csproj +++ b/src/RazorPagesProject/RazorPagesProject.csproj @@ -18,12 +18,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + - + diff --git a/src/RazorPagesProject/Resources/Pages/GitHubProfile.en.resx b/src/RazorPagesProject/Resources/Pages/GitHubProfile.en.resx index 2f4caf2..5eac620 100644 --- a/src/RazorPagesProject/Resources/Pages/GitHubProfile.en.resx +++ b/src/RazorPagesProject/Resources/Pages/GitHubProfile.en.resx @@ -106,4 +106,10 @@ Language + + Unable to fetch the GitHub profile. Please check the username and try again. + + + An unexpected error occurred. Please try again later. + diff --git a/src/RazorPagesProject/Resources/Pages/GitHubProfile.ja.resx b/src/RazorPagesProject/Resources/Pages/GitHubProfile.ja.resx index 563b27c..55dbc7e 100644 --- a/src/RazorPagesProject/Resources/Pages/GitHubProfile.ja.resx +++ b/src/RazorPagesProject/Resources/Pages/GitHubProfile.ja.resx @@ -106,4 +106,10 @@ 言語 + + GitHubプロフィールを取得できませんでした。ユーザー名を確認して、もう一度お試しください。 + + + 予期しないエラーが発生しました。後でもう一度お試しください。 +