A simple test for maybe doing events in the backgrounds between pages in Blazor.

feat: localize logging messages in NotificationReceiver and NotifyService

Changed files
+29 -11
Components
Services
+18 -6
Components/Pages/NotificationReceiver.razor
··· 16 16 @code { 17 17 private int currentCount = 0; 18 18 19 + /// <summary> 20 + /// Wird beim Initialisieren der Komponente aufgerufen und registriert den Benachrichtigungshandler. 21 + /// </summary> 19 22 protected override void OnInitialized() 20 23 { 21 - logger.LogInformation("NotificationReceiver initialized"); 24 + logger.LogInformation("NotificationReceiver initialisiert"); 22 25 notifyService.OnNotificationReceived += HandleNotification; 23 - logger.LogInformation("Event handler registered for notification events"); 26 + logger.LogInformation("Ereignishandler für Benachrichtigungen registriert"); 24 27 } 25 28 29 + /// <summary> 30 + /// Ereignishandler: Erhöht den Zähler bei eingehender Benachrichtigung und aktualisiert die UI. 31 + /// </summary> 32 + /// <remarks> 33 + /// Verwendet <see cref="InvokeAsync(Action)"/>, um einen sicheren UI-Refresh im richtigen Synchronisationskontext auszuführen. 34 + /// </remarks> 26 35 private async void HandleNotification() 27 36 { 28 - logger.LogInformation("Notification received"); 37 + logger.LogInformation("Benachrichtigung empfangen"); 29 38 currentCount++; 30 39 await InvokeAsync(StateHasChanged); 31 - logger.LogInformation("State updated. Current count: {Count}", currentCount); 40 + logger.LogInformation("Status aktualisiert. Aktueller Zähler: {Count}", currentCount); 32 41 } 33 42 43 + /// <summary> 44 + /// Deregistriert den Ereignishandler, wenn die Komponente freigegeben wird, um Speicherlecks zu vermeiden. 45 + /// </summary> 34 46 public void Dispose() 35 47 { 36 - logger.LogInformation("Disposing NotificationReceiver"); 48 + logger.LogInformation("NotificationReceiver wird freigegeben"); 37 49 notifyService.OnNotificationReceived -= HandleNotification; 38 - logger.LogInformation("Event handler unregistered for notification events"); 50 + logger.LogInformation("Ereignishandler für Benachrichtigungen deregistriert"); 39 51 } 40 52 }
+3 -3
Program.cs
··· 31 31 32 32 app.MapGet("/api/notify", ([FromServices] ILogger<Program> logger, [FromServices] NotifyService notifyService) => 33 33 { 34 - logger.LogInformation("Received request to raise event"); 34 + logger.LogInformation("Anfrage zum Auslösen eines Ereignisses erhalten"); 35 35 try 36 36 { 37 37 notifyService.SendNotification(); 38 38 } 39 39 catch (Exception ex) 40 40 { 41 - logger.LogError(ex, "Error occurred while raising event"); 41 + logger.LogError(ex, "Fehler beim Auslösen des Ereignisses"); 42 42 return Results.Problem(ex.Message); 43 43 } 44 - logger.LogInformation("Event raised successfully"); 44 + logger.LogInformation("Ereignis erfolgreich ausgelöst"); 45 45 return Results.Ok(); 46 46 }); 47 47
+8 -2
Services/NotifyService.cs
··· 4 4 { 5 5 public event Action? OnNotificationReceived; 6 6 7 + /// <summary> 8 + /// Sendet eine Benachrichtigung an alle registrierten Abonnenten, indem das Ereignis ausgelöst wird. 9 + /// </summary> 7 10 public void SendNotification() 8 11 { 9 - logger.LogInformation("Sending notification"); 12 + logger.LogInformation("Sende Benachrichtigung"); 10 13 NotificationReceivedHandler(); 11 - logger.LogInformation("Notification sent"); 14 + logger.LogInformation("Benachrichtigung gesendet"); 12 15 } 13 16 17 + /// <summary> 18 + /// Internes Hilfsmittel zum sicheren Auslösen des Ereignisses (ruft alle Handler auf, falls vorhanden). 19 + /// </summary> 14 20 private void NotificationReceivedHandler() => OnNotificationReceived?.Invoke(); 15 21 }