ASP.MVC Castle Nlog Count does not reset

149 views Asked by At

Currently using Castle Windsor IoC container and NLog as my logging facility. Everything is wired up and working except the Count field continues to increment across separate web requests. The install is very vanilla and so is the config.

My guess is that a new logger is not being created for every request but I have not been able to find a way to set a per web request life cycle in place on the logging facility.

Have been digging around the interweb and trying different install methods for about 8 hours now and am stuck.

current installer looks like this:

public class LoggingInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<LoggingFacility>(l => l.UseNLog());
    }
}

controller activator

public class WindsorHttpControllerActivator : IHttpControllerActivator
{
    private readonly IWindsorContainer _container;

    public WindsorHttpControllerActivator(IWindsorContainer container)
    {
        _container = container;
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        var controller = (IHttpController)_container.Resolve(controllerType);

        request.RegisterForDispose(new Release(() => _container.Release(controller)));

        return controller;
    }

    private class Release : IDisposable
    {
        private readonly Action _release;

        public Release(Action release)
        {
            _release = release;
        }

        public void Dispose()
        {
            _release();
        }
    }
}

global

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorHttpControllerActivator(_container));
    }

THE_PROBLEM: if i make 2 calls to a service on this website and add 5 logs per call the cound will go from 1->5 on thread 1 and then 6->10 on thread 2.

THE_DESIRED_RESULT: the expected result would be 1->5 on thread 1 and 1->5 on thread 2

Thanks in advance!

1

There are 1 answers

0
workabyte On BEST ANSWER

so I was able to get the desired result doing the following

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component
                .For<ILoggerFactory>()
                .ImplementedBy <NLogFactory>()
                .DependsOn(Dependency.OnValue("configFile", "nlog.config"))
                .LifestylePerWebRequest(),
            Component.For<ILogger>()
                .UsingFactoryMethod(
                    (kernel, model, context) =>
                    {
                        var logger = kernel.Resolve<ILoggerFactory>()
                            .Create(context.Handler.ComponentModel.ComponentName.Name);
                        return logger;
                    })
                .LifestylePerWebRequest()
            );
    }

Not a windsor guru so not really sure if there is anything lost by NOT using the LoggingFacility.

By keeping to the ILogger and ILoggerFactory I do maintain my ability to change out loggers on demand so I think I am good there