Implement enhanced static file caching and improve calendar event loading with detailed logging

This commit introduces a new static file caching strategy in Program.cs, optimizing cache headers for Blazor assets to improve performance and ensure fresh content after deployments. Additionally, the Calendar component in Index.razor has been updated to include comprehensive logging for event loading, handling null occurrences, and error management during calendar item creation. The CalendarEventItem model is also initialized to prevent null reference issues. These changes enhance the application's reliability and user experience.
This commit is contained in:
2026-01-05 13:21:03 -05:00
parent 01056401e5
commit 2aaefb2491
3 changed files with 134 additions and 17 deletions
+104 -16
View File
@@ -4,7 +4,9 @@
@using WebApp.Models
@using WebApp.Services
@using Heron.MudCalendar
@using Microsoft.Extensions.Logging
@inject IEventOccurrenceService EventOccurrenceService
@inject ILogger<Index> Logger
<PageHeader Title="Event Calendar" Description="View competition schedules and event occurrences" Icon="@AppIcons.EventCalendar">
<ActionButtons>
@@ -23,7 +25,7 @@
<MudCalendar T="CalendarEventItem"
Items="_calendarItems"
View="CalendarView.Day"
CurrentDay=_calendarDate
CurrentDay="@_calendarDate"
/>
}
</MudPaper>
@@ -39,29 +41,115 @@
private async Task LoadCalendarEvents()
{
var occurrences = await EventOccurrenceService.GetEventOccurrencesAsync();
_calendarItems = occurrences
.Select(occ => new CalendarEventItem(occ))
.ToList();
try
{
Logger.LogInformation("Loading calendar events");
var occurrences = await EventOccurrenceService.GetEventOccurrencesAsync();
if (occurrences == null)
{
Logger.LogWarning("Service returned null occurrences");
_calendarItems = new List<CalendarEventItem>();
return;
}
// Find the next date with events
_calendarDate = GetNextDateWithEvents();
Logger.LogDebug("Received {Count} occurrences from service", occurrences.Count());
var items = new List<CalendarEventItem>();
foreach (var occ in occurrences)
{
try
{
if (occ == null)
{
Logger.LogWarning("Null occurrence found, skipping");
continue;
}
if (string.IsNullOrEmpty(occ.Name))
{
Logger.LogWarning("Occurrence with Id={Id} has null or empty Name", occ.Id);
}
var calendarItem = new CalendarEventItem(occ, occ.EventDefinition);
items.Add(calendarItem);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error creating CalendarEventItem for occurrence Id={Id}, Name={Name}",
occ?.Id, occ?.Name);
// Continue processing other items
}
}
_calendarItems = items;
Logger.LogInformation("Created {Count} calendar items from {OccurrenceCount} occurrences",
_calendarItems.Count, occurrences.Count());
// Find the next date with events
_calendarDate = GetNextDateWithEvents();
}
catch (Exception ex)
{
Logger.LogError(ex, "Error loading calendar events");
_calendarItems = new List<CalendarEventItem>();
}
finally
{
StateHasChanged();
}
}
private DateTime GetNextDateWithEvents()
{
if (_calendarItems == null || !_calendarItems.Any())
try
{
if (_calendarItems == null || !_calendarItems.Any())
{
Logger.LogDebug("No calendar items available, returning today's date");
return DateTime.Today;
}
var today = DateTime.Today;
var nextEvent = _calendarItems
.Where(item =>
{
try
{
return item != null && item.Start.Date >= today;
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Error checking item date, skipping item");
return false;
}
})
.OrderBy(item => item.Start)
.FirstOrDefault();
if (nextEvent != null)
{
return nextEvent.Start.Date;
}
// Fallback to first event if no future events
var firstEvent = _calendarItems
.Where(item => item != null)
.OrderBy(item => item.Start)
.FirstOrDefault();
if (firstEvent != null)
{
return firstEvent.Start.Date;
}
return DateTime.Today;
}
catch (Exception ex)
{
Logger.LogError(ex, "Error in GetNextDateWithEvents");
return DateTime.Today;
}
var today = DateTime.Today;
var nextEvent = _calendarItems
.Where(item => item.Start.Date >= today)
.OrderBy(item => item.Start)
.FirstOrDefault();
return nextEvent?.Start.Date ?? _calendarItems.OrderBy(item => item.Start).First().Start.Date;
}
}