public void LayoutDynamicRenderExceptionTypeTest() { // Arrange Layout <Type> layout = "${exception:format=type:norawvalue=true}"; var exception = new System.ApplicationException("Test"); var stringBuilder = new System.Text.StringBuilder(); // Act var logevent = LogEventInfo.Create(LogLevel.Info, null, exception, null, ""); var exceptionType = layout.RenderTypedValue(logevent, stringBuilder, null); stringBuilder.Length = 0; // Assert Assert.Equal(exception.GetType(), exceptionType); Assert.Same(exceptionType, layout.RenderTypedValue(logevent, stringBuilder, null)); }
public void AfterThrowing(ApplicationException aex) { Count(aex.GetType().Name); }
public void ProcessExceptionTest() { Type[] exceptionTypes = new Type[] { typeof(OutOfMemoryException), typeof(DataServiceException), typeof(FormatException), typeof(DataServiceException), }; // At-End is currently always true, because the IQueryable caching // doesn't give us a good point to fail before content is written out. CombinatorialEngine engine = CombinatorialEngine.FromDimensions( new Dimension(CustomDataContext.ExceptionTypeArgument, exceptionTypes), new Dimension(CustomDataContext.ExceptionAtEndArgument, new object[] { true }), new Dimension("Format", SerializationFormatData.Values), new Dimension("WebServerLocation", new object[] { WebServerLocation.InProcess, WebServerLocation.Local })); TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values) { Type exceptionType = (Type)values[CustomDataContext.ExceptionTypeArgument]; bool exceptionAtEnd = (bool)values[CustomDataContext.ExceptionAtEndArgument]; SerializationFormatData format = (SerializationFormatData)values["Format"]; WebServerLocation location = (WebServerLocation)values["WebServerLocation"]; // The local web server doesn't handle OOF gracefully - skip that case. if (exceptionType == typeof(OutOfMemoryException) && location == WebServerLocation.Local) { return; } // No binary properties in the model. if (format.Name == "Binary") return; // We need at least 1024 bytes to be written out for the default // StreamWriter used by the XmlTextWriter to flush out (at which point // we can assume that throwing at the end of the stream caused // a "partial send"). // // However the implementation ends up using an XmlUtf8RawTextWriterIndent // object, with a BUFSIZE of 0x1800 as declared on XmlUtf8RawTextWriter. // // (0x1800 / "Customer 1".Length) + 1 is ideal, but we can make do with much less. int customerCount = (0xA0 / "Customer 1".Length) + 1; values[CustomDataContext.CustomerCountArgument] = customerCount; using (TestWebRequest request = TestWebRequest.CreateForLocation(location)) { request.DataServiceType = typeof(CustomDataContext); request.TestArguments = values; request.Accept = format.MimeTypes[0]; request.RequestUriString = (format.Name == "Text") ? "/Customers(" + customerCount + ")/ID/$value" : "/Customers"; Trace.WriteLine("Requesting " + request.RequestUriString); Stream response = new MemoryStream(); Exception thrownException = null; try { request.SendRequest(); thrownException = new ApplicationException("No exception actually thrown."); } catch (Exception exception) { thrownException = exception; } // InProcess always throws WebException. Look in the inner exception for the right exception type. if (location == WebServerLocation.InProcess && !format.IsPrimitive && exceptionType != typeof(OutOfMemoryException)) { Assert.AreEqual(typeof(WebException), thrownException.GetType(), "InProcess should always throw WebException - Look in TestServiceHost.ProcessException"); thrownException = thrownException.InnerException; } // Exception may be wrapped by TargetInvocationException. if (thrownException is TargetInvocationException) { thrownException = thrownException.InnerException; } TestUtil.CopyStream(request.GetResponseStream(), response); response.Position = 0; if (exceptionAtEnd && !format.IsPrimitive) { // for inprocess, there will be no exception in the payload if (location == WebServerLocation.InProcess) { // Verify the exception type Assert.AreEqual(exceptionType, thrownException.GetType(), "Exception type did not match"); return; } Assert.IsTrue(HasContent(response), "HasContent(response)"); Assert.IsTrue(String.Equals(request.Accept, TestUtil.GetMediaType(request.ResponseContentType), StringComparison.OrdinalIgnoreCase)); if (exceptionType != typeof(OutOfMemoryException)) { string responseText = new StreamReader(response).ReadToEnd(); TestUtil.AssertContains(responseText, "error"); TestUtil.AssertContains(responseText, "message"); } Assert.IsTrue(thrownException is ApplicationException, "No exception thrown."); } else { if (exceptionType == typeof(OutOfMemoryException)) { if (location == WebServerLocation.InProcess) { Assert.IsTrue(thrownException is OutOfMemoryException, "thrownException is OutOfMemoryException"); Assert.IsTrue(exceptionAtEnd || !HasContent(response), "exceptionAtEnd || !HasContent(response)"); } else { Assert.IsTrue(thrownException is WebException, "thrownException is WebException"); } } else { Assert.IsTrue(thrownException is WebException, "thrownException is WebException"); Assert.IsTrue(HasContent(response), "HasContent(response)"); string expected = (location == WebServerLocation.InProcess) ? "text/plain" : "application/xml"; Assert.AreEqual(expected, TestUtil.GetMediaType(request.ResponseContentType)); } } } }); }