Autofac Smart Interceptor leverages Autofac Dependency Injection to inject interceptors to registered interfaces. For example the LogInterceptor will output all method calls to the specified output (Console, Textfile, etc)
https://www.nuget.org/packages/Autofac.SmartInterceptors
- LogInterceptor:
This interceptor takes a TextWriter in the contructor (Console.Out, File, etc) and ouputs all the method calls.
Log Example:
[10:20:11] [INFO] MyClass.MyMethod(param1: "value", param2: "value)
- SmartInterceptor
This interceptor takes a
beforeMethodCallAction
andafterMethodCallAction
so you can have your own implementation before and after a method call.
Attach the log interceptor to Autofac. In this case we will register the LogInterceptor
with output to Console.Out
so all the methods in the class TestClass
will be logged to Console.Out
.
//create autofac container
var builder = new ContainerBuilder();
//register the class you want to intercept method calls
builder.RegisterType<TestClass>().As<ITestClass>().Intercept();
//attach interceptors
builder.AttachInterceptorsToRegistrations(new LogInterceptor(Console.Out));
//build container
var container = builder.Build();
Check the Autofac.SmartInterceptors.Tests for more examples on how to use it.