Wraps each method call and log any exceptions using IExceptionLogger.
Will per default decorate all services. Do note that the exceptions are rethrown after the logging (without affecting the callstack)
Inheritance: CastleDecorator
コード例 #1
0
        public void TestLoggingDecorator()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<TotalFailure>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var tmp = container.Resolve<TotalFailure>();
            Assert.Throws<InvalidOperationException>(() => tmp.Fail("Big!"));
            Assert.IsType<InvalidOperationException>(_exception);
        }
コード例 #2
0
        public void TestDecorateHiearchy()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Decorated1>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated2>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated3>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var all = container.ResolveAll<IShouldBeDecorated>();
            Assert.True(all.All(x => x.GetType().Name.Contains("Proxy")));
        }