コード例 #1
0
        public void SaveBinaryExceptionDoesNotThrow(Type exceptionType)
        {
            var errorLog = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123
            };
            var exception          = exceptionType.GetConstructor(Type.EmptyTypes)?.Invoke(null) as System.Exception;
            var errorLogFilename   = errorLog.Id + ".json";
            var serializedErrorLog = LogSerializer.Serialize(errorLog);
            var exceptionFilename  = errorLog.Id + ".exception";
            var mockDirectory      = Mock.Of <Directory>();
            var mockStream         = new System.IO.MemoryStream();

            Mock.Get(mockDirectory).Setup(d => d.FullName).Returns("Errors");

            // Cause stream to fail writing
            mockStream.Dispose();

            // Given we succeed saving log file but fail saving exception file.
            ErrorLogHelper.Instance._crashesDirectory = mockDirectory;

            // When we save files.
            ErrorLogHelper.SaveErrorLogFiles(exception, errorLog);

            // Then it does not throw.
            Mock.Get(mockDirectory).Verify(d => d.CreateFile(errorLogFilename, serializedErrorLog));
            Mock.Get(mockDirectory).Verify(d => d.CreateFile(exceptionFilename, exception.ToString()));
        }
コード例 #2
0
        public void SaveErrorLogFileWithObfuscateUserName()
        {
            // Prepared data.
            Constants.UserName = "******";
            var pathWithUserName = $"C:\\{Constants.UserName}\\some\\path";
            var errorLog         = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123,
                Exception = new Ingestion.Models.Exception
                {
                    StackTrace = pathWithUserName
                }
            };
            var expectedException = new System.Exception(pathWithUserName);

            // Call method.
            ErrorLogHelper.SaveErrorLogFiles(expectedException, errorLog);

            // Verify obfuscated data.
            var fileExceptionContent = ErrorLogHelper.GetStoredExceptionFile(errorLog.Id).ReadAllText();
            var fileErrorContant     = ErrorLogHelper.GetStoredErrorLogFile(errorLog.Id).ReadAllText();

            Assert.IsFalse(fileErrorContant.Contains(Constants.UserName));
            Assert.IsFalse(fileExceptionContent.Contains(Constants.UserName));
        }
コード例 #3
0
        public void SaveErrorLogFileDoesNotThrow(Type exceptionType)
        {
            // Use reflection to create an exception of the given C# type.
            var exception = exceptionType.GetConstructor(Type.EmptyTypes)?.Invoke(null) as System.Exception;
            var errorLog  = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123
            };
            var mockDirectory = Mock.Of <Directory>();

            ErrorLogHelper.Instance._crashesDirectory = mockDirectory;
            Mock.Get(mockDirectory).Setup(d => d.EnumerateFiles(It.IsAny <string>())).Throws(exception);
            ErrorLogHelper.SaveErrorLogFiles(null, errorLog);

            // No exception should be thrown.
        }
コード例 #4
0
        public void SaveErrorLogFiles()
        {
            var errorLog = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123
            };
            var expectedException  = new System.Exception("test");
            var errorLogFilename   = errorLog.Id + ".json";
            var serializedErrorLog = LogSerializer.Serialize(errorLog);
            var exceptionFilename  = errorLog.Id + ".exception";
            var mockStream         = new System.IO.MemoryStream();
            var mockDirectory      = Mock.Of <Directory>();

            ErrorLogHelper.Instance._crashesDirectory = mockDirectory;
            ErrorLogHelper.SaveErrorLogFiles(expectedException, errorLog);

            Mock.Get(mockDirectory).Verify(d => d.CreateFile(errorLogFilename, serializedErrorLog));
            Mock.Get(mockDirectory).Verify(d => d.CreateFile(exceptionFilename, expectedException.ToString()));
        }
コード例 #5
0
        public void SaveBinaryExceptionDoesNotThrow(Type exceptionType)
        {
            var errorLog = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123
            };
            var exception          = exceptionType.GetConstructor(Type.EmptyTypes)?.Invoke(null) as System.Exception;
            var errorLogFilename   = errorLog.Id + ".json";
            var serializedErrorLog = LogSerializer.Serialize(errorLog);
            var exceptionFilename  = errorLog.Id + ".exception";
            var mockDirectory      = Mock.Of <Directory>();
            var mockStream         = new System.IO.MemoryStream();

            Mock.Get(mockDirectory).Setup(d => d.FullName).Returns("Errors");
            var    expectedFullFileName = $"Errors\\{exceptionFilename}";
            string actualFullFileName   = null;

            System.IO.FileMode?actualFileMode = null;

            // Cause stream to fail writing
            mockStream.Dispose();

            // Given we succeed saving log file but fail saving exception file.
            ErrorLogHelper.Instance._crashesDirectory = mockDirectory;
            ErrorLogHelper.Instance.NewFileStream     = (name, mode) =>
            {
                actualFullFileName = name;
                actualFileMode     = mode;
                return(mockStream);
            };

            // When we save files.
            ErrorLogHelper.SaveErrorLogFiles(exception, errorLog);

            // Then it does not throw.
            Assert.AreEqual(expectedFullFileName, actualFullFileName);
            Assert.AreEqual(System.IO.FileMode.Create, actualFileMode);
            Mock.Get(mockDirectory).Verify(d => d.CreateFile(errorLogFilename, serializedErrorLog));
        }
コード例 #6
0
        public void SaveErrorLogFiles()
        {
            var errorLog = new ManagedErrorLog
            {
                Id        = Guid.NewGuid(),
                ProcessId = 123
            };
            var expectedException  = new System.Exception("test");
            var errorLogFilename   = errorLog.Id + ".json";
            var serializedErrorLog = LogSerializer.Serialize(errorLog);
            var exceptionFilename  = errorLog.Id + ".exception";
            var mockStream         = new System.IO.MemoryStream();
            var mockDirectory      = Mock.Of <Directory>();

            Mock.Get(mockDirectory).Setup(d => d.FullName).Returns("Errors");
            var    expectedFullFileName = $"Errors\\{exceptionFilename}";
            string actualFullFileName   = null;

            System.IO.FileMode?actualFileMode = null;

            ErrorLogHelper.Instance._crashesDirectory = mockDirectory;
            ErrorLogHelper.Instance.NewFileStream     = (name, mode) =>
            {
                actualFullFileName = name;
                actualFileMode     = mode;
                return(mockStream);
            };
            ErrorLogHelper.SaveErrorLogFiles(expectedException, errorLog);

            Assert.AreEqual(expectedFullFileName, actualFullFileName);
            Assert.AreEqual(System.IO.FileMode.Create, actualFileMode);
            Mock.Get(mockDirectory).Verify(d => d.CreateFile(errorLogFilename, serializedErrorLog));

            // Check what was serialized.
            var actualException = new BinaryFormatter().Deserialize(new System.IO.MemoryStream(mockStream.ToArray()));

            Assert.IsInstanceOfType(actualException, expectedException.GetType());
            Assert.AreEqual(expectedException.Message, ((System.Exception)actualException).Message);
        }
コード例 #7
0
        private static void OnUnhandledExceptionOccurred(object sender, UnhandledExceptionOccurredEventArgs args)
        {
            var errorLog = ErrorLogHelper.CreateErrorLog(args.Exception);

            ErrorLogHelper.SaveErrorLogFiles(args.Exception, errorLog);
        }