相关疑难解决方法(0)

温莎 - 从容器中拉出瞬态物体

如何从容器中拉出瞬态物体?我是否必须在容器中注册它们并注入需要类的构造函数?将所有内容注入构造函数中感觉不太好.也只是为了一个类,我不想创建一个TypedFactory并将工厂注入需要的类.

我想到的另一个想法是根据需要"新"起来.但我也在我的Logger所有类中注入一个组件(通过属性).因此,如果我新建它们,我将不得不手动实例化Logger这些类.如何继续为我的所有课程使用容器?

记录器注入:我的大多数类都Logger定义了属性,除非存在继承链(在这种情况下,只有基类具有此属性,并且所有派生类都使用该属性).当这些通过Windsor容器实例化时,它们会将我的实现ILogger注入其中.

//Install QueueMonitor as Singleton
Container.Register(Component.For<QueueMonitor>().LifestyleSingleton());
//Install DataProcessor as Trnsient
Container.Register(Component.For<DataProcessor>().LifestyleTransient());

Container.Register(Component.For<Data>().LifestyleScoped());

public class QueueMonitor
{
    private dataProcessor;

    public ILogger Logger { get; set; }

    public void OnDataReceived(Data data)
    {
        //pull the dataProcessor from factory    
        dataProcessor.ProcessData(data);
    }
}

public class DataProcessor
{
    public ILogger Logger { get; set; }

    public Record[] ProcessData(Data data)
    {
        //Data can have multiple Records
        //Loop through the data and create new set …
Run Code Online (Sandbox Code Playgroud)

.net dependency-injection castle-windsor ioc-container

57
推荐指数
1
解决办法
2万
查看次数

命名记录器上的log4net策略?

我通常在每个班级中声明以下内容:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Run Code Online (Sandbox Code Playgroud)

并使用每个类中的静态成员来记录不同的级别(信息,调试等)

我在某个地方看到并且一直在使用它有点盲目,推断设置足够灵活,可以帮助我按命名空间过滤并记录各个类型,如果我想解决生产问题而不是.

但我很少使用精细伐木的"水平".所以,我想看看其他人在使用什么.您是否使用上述内容,因为我感觉很多人正在使用它,或者您是否创建了命名记录器,如"debug","trace","error","moduleA"等,并在不同类型之间共享记录器,组装?

logging log4net

5
推荐指数
2
解决办法
9715
查看次数

使用Autofac将NLog注入Web Api控制器

我正在尝试使用Autofac添加NLog我的Web Api项目。但是我有问题。

从NuGet安装NLog软件包后,我将以下文件添加到我的项目中。(由于遵循不同的帖子和示例,可能会造成混淆)

ILogger.cs

 public interface ILogger {
        void Debug(string message);
        void Trace(string message);
        void Info(string message);
        void Warning(string message);
        void Error(string message);
        void Error(string message, Exception exception);
        void Fatal(string message);
        void Fatal(string message, Exception exception);
    }
Run Code Online (Sandbox Code Playgroud)

LoggerAttribute.cs

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Class)]
    public class LoggerAttribute : Attribute {
        public readonly string Name;

        public LoggerAttribute(string name) {
            Name = name;
        }
    }
Run Code Online (Sandbox Code Playgroud)

NLogger.cs

public class NLogger : ILogger {
        private readonly NLog.Logger logger;

        public NLogger(Type loggerType) {
             logger = LogManager.GetLogger(loggerType.FullName);
        }

        public …
Run Code Online (Sandbox Code Playgroud)

nlog autofac asp.net-web-api

3
推荐指数
1
解决办法
4391
查看次数