Using structuremap with log4net wrapper

后端 未结 3 1325
南笙
南笙 2020-12-13 11:34

I have the following interface:

public interface ILogger
{
    void Debug(string message, params object[] values);
    void Info(string message, params objec         


        
3条回答
  •  温柔的废话
    2020-12-13 11:49

    We use a similar ILogger wrapper around log4net and typically use constructor injection. We use an interceptor as a factory method responsible for creating the Logger. Here is our typical registry for logging setup.

    public class CommonsRegistry : Registry
    {
        public CommonsRegistry()
        {
            For()
                .AlwaysUnique()
                .TheDefault.Is.ConstructedBy(s =>
                {
                    if (s.ParentType == null)
                        return new Log4NetLogger(s.BuildStack.Current.ConcreteType);
    
                    return new Log4NetLogger(s.ParentType);
                });
    
            var applicationPath = Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location);
            var configFile = new FileInfo(Path.Combine(applicationPath, "log4net.config"));
            XmlConfigurator.ConfigureAndWatch(configFile);
        }
    }
    

    The parent type null check is necessary when there are dependencies on concrete types.

    The rest is optional log4net setup stuff.

    One thing I do like about this setup is the ability to use a null loggers for unit testing.

提交回复
热议问题