private async Task LogErrorAsync(Exception e, string code) { ExceptionRunningUsersCode log = new ExceptionRunningUsersCode { ExceptionText = e.ToString(), ExceptionDateTime = DateTimeOffset.UtcNow, UserCode = $"<Not from user code>{Environment.NewLine}{code}" }; log.UserId = userServices.GetCurrentUserId(); try { // Log to database await sqlRep.InsertExceptionLogAsync(log); } catch { // Ignore exceptions - no way of logging them if the logger fails, but want // to allow program flow to continue anyway in order to show message to user } }
public async Task LogException(Exception e, string code) { ExceptionRunningUsersCode log = new ExceptionRunningUsersCode { ExceptionText = e.ToString(), ExceptionDateTime = DateTimeOffset.UtcNow, UserCode = code, UserId = userId }; try { await rep.InsertExceptionLogAsync(log); } catch { // Any exceptions, try again without the user id // Probably could narrow this down to only foreign key violations... log.UserId = null; await rep.InsertExceptionLogAsync(log); } }
// Exception logs public async Task InsertExceptionLogAsync(ExceptionRunningUsersCode log) { context.ExceptionLogs.Add(log); await context.SaveChangesAsync(); }