NLog GetCurrentClassLogger() NullReferenceException using StructureMap (Full Trust)

只愿长相守 提交于 2019-12-02 23:34:14
Petrus Theron

Turns out it's not a trust issue, but an IoC issue. Thanks to an NInject solution for steering me in the right direction.

Solution

For your logging wrapper, use a constructor that accepts a string parameter:

public class NLogLogger : ILogger
{
    private Logger _logger;

    public NLogLogger(string currentClassName)
    {
        _logger = LogManager.GetLogger(currentClassName);
    }
    ...
}

NLog StructureMap Config:

For<ILogger>().Use<NLogLogger>()
    .Ctor<string>("currentClassName")
    .Is(x => x.BuildStack.Root.RequestedType.FullName);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!