diff --git a/Bootstrap.DataAccess/Exceptions.cs b/Bootstrap.DataAccess/Exceptions.cs index 8eaab64a..c46dea98 100644 --- a/Bootstrap.DataAccess/Exceptions.cs +++ b/Bootstrap.DataAccess/Exceptions.cs @@ -1,4 +1,4 @@ -using Longbow.Web.Mvc; +using Longbow.Web.Mvc; using PetaPoco; using System; using System.Collections.Generic; @@ -95,19 +95,24 @@ namespace Bootstrap.DataAccess } loopEx = loopEx.InnerException; } - DbManager.Create().Insert(new Exceptions + try { - AppDomainName = AppDomain.CurrentDomain.FriendlyName, - ErrorPage = errorPage, - UserId = additionalInfo?["UserId"], - UserIp = additionalInfo?["UserIp"], - ExceptionType = ex.GetType().FullName, - Message = ex.Message, - StackTrace = ex.StackTrace, - LogTime = DateTime.Now, - Category = category - }); - ClearExceptions(); + // 防止数据库写入操作失败后陷入死循环 + DbManager.Create().Insert(new Exceptions + { + AppDomainName = AppDomain.CurrentDomain.FriendlyName, + ErrorPage = errorPage, + UserId = additionalInfo?["UserId"], + UserIp = additionalInfo?["UserIp"], + ExceptionType = ex.GetType().FullName, + Message = ex.Message, + StackTrace = ex.StackTrace, + LogTime = DateTime.Now, + Category = category + }); + ClearExceptions(); + } + catch { } return true; } diff --git a/UnitTest/Bootstrap.DataAccess/RollbackTest.cs b/UnitTest/Bootstrap.DataAccess/RollbackTest.cs index 693df18c..e23f5e40 100644 --- a/UnitTest/Bootstrap.DataAccess/RollbackTest.cs +++ b/UnitTest/Bootstrap.DataAccess/RollbackTest.cs @@ -79,7 +79,7 @@ namespace Bootstrap.DataAccess var newUser = new User() { UserName = "U_Reset", DisplayName = "UnitTest", ApprovedTime = DateTime.Now, ApprovedBy = "System", Password = "1", Description = "UnitTest", RegisterTime = DateTime.Now }; Assert.True(UserHelper.Save(newUser)); Assert.True(UserHelper.ForgotPassword(new ResetUser() { DisplayName = "UnitTest", Reason = "UnitTest", ResetTime = DateTime.Now, UserName = newUser.UserName })); - Assert.ThrowsAny(() => TestHelper.RevokeUserMapper(() => new User().ResetPassword(newUser.UserName, "123789"))); + Assert.ThrowsAny(() => TestHelper.RevokePocoMapper(() => new User().ResetPassword(newUser.UserName, "123789"))); Assert.True(UserHelper.Delete(new string[] { newUser.Id })); } @@ -106,5 +106,11 @@ namespace Bootstrap.DataAccess { Assert.ThrowsAny(() => TestHelper.RevokeMapper(() => new User().SaveByGroupId("1", new string[] { "1" }))); } + + [Fact] + public void Exceptions_Log() + { + TestHelper.RevokePocoMapper(() => new Exceptions().Log(new Exception(), null)); + } } } diff --git a/UnitTest/TestHelper.cs b/UnitTest/TestHelper.cs index 65675192..2435f7c9 100644 --- a/UnitTest/TestHelper.cs +++ b/UnitTest/TestHelper.cs @@ -103,10 +103,10 @@ namespace UnitTest } } - public static void RevokeUserMapper(Action callback) + public static void RevokePocoMapper(Action callback) { var foo = new FooMapper(); - Mappers.Register(typeof(User), foo); + Mappers.Register(typeof(T), foo); try { callback(); } catch (Exception ex) { throw ex; } finally