Example #1
0
        public async Task request_monitor_handles_exceptions_but_does_not_swallow_them_by_default(bool swallow)
        {
            var text   = new TextGrandOutputHandlerConfiguration();
            var config = new GrandOutputConfiguration();

            config.AddHandler(text);
            GrandOutput.EnsureActiveDefault(config);
            int rootExceptionCount = 0;

            try
            {
                var b = Tester.WebHostBuilderFactory.Create(null, null,
                                                            services =>
                {
                    services.AddSingleton <StupidService>();
                },
                                                            app =>
                {
                    app.Use(async(context, next) =>
                    {
                        try
                        {
                            await next.Invoke();
                        }
                        catch
                        {
                            ++rootExceptionCount;
                            context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                        }
                    });
                    app.UseRequestMonitor(o => { o.SwallowErrors = swallow; });
                    app.UseMiddleware <StupidMiddleware>();
                });
                using (var client = new TestServerClient(new TestServer(b)))
                {
                    using (HttpResponseMessage bug = await client.Get("?bug"))
                    {
                        bug.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
                        text.GetText().Should().Contain("Bug!");
                    }
                    using (HttpResponseMessage asyncBug = await client.Get("?asyncBug"))
                    {
                        asyncBug.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
                        text.GetText().Should().Contain("AsyncBug!");
                    }
                }
            }
            finally
            {
                GrandOutput.Default.Dispose();
            }
            if (swallow)
            {
                Assert.That(rootExceptionCount, Is.EqualTo(0));
            }
            else
            {
                Assert.That(rootExceptionCount, Is.EqualTo(2));
            }
        }
        bool IGrandOutputHandler.ApplyConfiguration(IActivityMonitor m, IHandlerConfiguration c)
        {
            TextGrandOutputHandlerConfiguration config = c as TextGrandOutputHandlerConfiguration;

            if (config != null)
            {
                _config = config;
                return(true);
            }
            return(false);
        }
        ValueTask <bool> IGrandOutputHandler.ApplyConfigurationAsync(IActivityMonitor m, IHandlerConfiguration c)
        {
            TextGrandOutputHandlerConfiguration config = c as TextGrandOutputHandlerConfiguration;

            if (config != null)
            {
                _config = config;
                return(ValueTask.FromResult(true));
            }
            return(ValueTask.FromResult(false));
        }
Example #4
0
 TextGrandOutputHandlerConfiguration(TextGrandOutputHandlerConfiguration origin)
 {
     _origin = origin;
 }
 /// <summary>
 /// Initializes a new <see cref="TextGrandOutputHandler"/>.
 /// </summary>
 /// <param name="config">The configuration object.</param>
 public TextGrandOutputHandler(TextGrandOutputHandlerConfiguration config)
 {
     _config = config;
 }
 /// <summary>
 /// Initializes a new <see cref="TextGrandOutputHandler"/>.
 /// </summary>
 /// <param name="config">The configuration object.</param>
 public TextGrandOutputHandler(TextGrandOutputHandlerConfiguration config)
 {
     _config  = config;
     _builder = new StringBuilder();
 }