Microsoft. Practices. Unity injects different Logger, microsoft. practices
Scenario: third-party log frameworks are often referenced when we create projects to help us record logs. Log components are mainly used for auditing, tracking, and debugging. Let's just talk about the most commonly used log component log4net. This is a very many log component in the. NET industry.
At the same time, we often use IoC technology to reduce the coupling between projects and modules, such as Microsoft. practices. unity (of course, Autofac is also very useful ).
We know the advantages of log4net clearly, and the configuration is very simple and complete. It can provide different log levels, recorders, and organization forms ......
For example:
var log = LogManager.GetLogger("User");
However, when we use IoC to inject a logger object, it will be difficult. I want to inject different logstores into different classes, this makes it easy for me to selectively Configure which classes and levels of logs need to be output.
For example:
public class UserService { public UserRepository Repository { get; } public ILog Log { get; set; } public UserService(UserRepository repository,ILog log) { Repository = repository; Log = log; } } public class UserRepository { public ILog Log { get; } public UserRepository(ILog log) { Log = log; } }
What I want is to inject LogManager. GetLogger (typeof (UserRepository) to UserRepository ));
What I want is to inject LogManager. GetLogger (typeof (UserService) into UserService ));
In this way, when writing logs in UserRepository and UserService, they are respectively written into different log recorders. I can easily control which logs are collected.
What should we do?
I found a lot of information and did not find the context for Microsoft. Practices. Unity to access the dependency parsing. I hope that the context can find the type of object requesting ILog.
Finally, we found a solution in the source code discussion area of Microsoft. Practices. Unity. We encapsulated the solution to simplify similar operations. The test code is as follows:
using NUnit.Framework;using System;using System.Diagnostics;using System.Threading.Tasks;using log4net;namespace Microsoft.Practices.Unity.Tracking.Tests{ [TestFixture] public class TrackingInjectionFactoryTests { public IUnityContainer Container { get; set; } [SetUp] public void Initialize() { Trace.Listeners.Add(new ConsoleTraceListener()); Container = new UnityContainer(); Container.Tracking(); Container.RegisterType<UserService>(); Container.RegisterType<ILog>(new TrackingInjectionFactory((container, context, policy) => LogManager.GetLogger(policy.RequestType?.Name ?? "null"))); } [Test] public void TrackingInjectionFactoryTest() { Parallel.For(0, 1, new ParallelOptions { MaxDegreeOfParallelism = 10 }, i => { var userService = this.Container.CreateChildContainer().Resolve<UserService>(); Trace.WriteLine(userService.Log.Logger.Name, "UserService.Log.Logger.Name"); Trace.WriteLine(userService.Repository.Log.Logger.Name, "UserService.Repository.Log.Logger.Name"); }); var action = new Action(() => { var log = this.Container.CreateChildContainer().Resolve<ILog>(); Trace.WriteLine(log.Logger.Name, "Logger.Name"); }); action(); var asyncResult = action.BeginInvoke(null, null); action.EndInvoke(asyncResult); } public class UserService { public UserRepository Repository { get; } public ILog Log { get; set; } public UserService(UserRepository repository, ILog log) { Repository = repository; Log = log; } } public class UserRepository { public ILog Log { get; } public UserRepository(ILog log) { Log = log; } } }}
Source code: https://github.com/echofool/Microsoft.Practices.Unity.Tracking
Forgive me for being lazy and don't want to explain too much...