Skip to content

Commit

Permalink
catch and log history exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
henryjump committed Oct 6, 2023
1 parent a6a2f45 commit 6c84ee7
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions uSync.History/uSyncHistoryNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Security;
Expand All @@ -15,12 +16,18 @@ internal class uSyncHistoryNotificationHandler
private readonly IHostingEnvironment _hostingEnvironment;
private readonly SyncFileService _syncFileService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly ILogger<uSyncHistoryNotificationHandler> _logger;

public uSyncHistoryNotificationHandler(SyncFileService syncFileService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor, IHostingEnvironment hostingEnvironment)
public uSyncHistoryNotificationHandler(
SyncFileService syncFileService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IHostingEnvironment hostingEnvironment,
ILogger<uSyncHistoryNotificationHandler> logger)
{
_syncFileService = syncFileService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_hostingEnvironment = hostingEnvironment;
_logger = logger;
}

public void Handle(uSyncImportCompletedNotification notification)
Expand Down Expand Up @@ -49,24 +56,31 @@ public void Handle(uSyncExportCompletedNotification notification)

private void SaveActions(IEnumerable<uSyncAction> actions, string method, int total)
{
var historyInfo = new HistoryInfo
try
{
Actions = actions,
Date = DateTime.Now,
Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process",
Method = method,
Total = total,
Changes = actions.CountChanges()
};
var historyInfo = new HistoryInfo
{
Actions = actions,
Date = DateTime.Now,
Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process",
Method = method,
Total = total,
Changes = actions.CountChanges()
};

var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented);
var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented);

var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath);
var historyFile = Path.Combine(rootFolder, "uSync", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json");
var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath);
var historyFile = Path.Combine(rootFolder, "uSync", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json");

_syncFileService.CreateFoldersForFile(historyFile);
_syncFileService.CreateFoldersForFile(historyFile);

_syncFileService.SaveFile(historyFile, historyJson);
_syncFileService.SaveFile(historyFile, historyJson);
}
catch(Exception ex)
{
_logger.LogWarning(ex, "Failed to save history.");
}
}
}
}

0 comments on commit 6c84ee7

Please sign in to comment.