Multi-tenant logging in ASP.NET MVC application

不羁岁月 提交于 2019-12-07 18:15:58

问题


I have a multi-tenant application in ASP.NET MVC 5 that can accept client logins from multiple companies. Currently, I'm using log4net to log to files, but it places all the logs from all the companies in one file. Ideally, I would like to separate out the log files so that the logs for each company resides in its own folder.

I see from this question that I can programmatically create additional appenders, which then can log to different log files. But that would mean that for each instance of a class, I would have to keep calling

ILog log = LogManager.GetLogger("CompanyA");

to get the correct logger, right? Is there a better way to do so? I'm also open to using another logger if need be.

Thanks.


回答1:


Do you use an Ioc container in your application? I was able to solve a similar problem using AutoFac MultitenantContainer. Steps that you need to follow

  1. define the strategy you would use to identify each tenant.
  2. Register a generic logger interface with Autofac container
  3. For each tenant register specific logger.

your code could look like (extract from Autofac wiki)

    var tenantIdStrategy = new RequestParameterTenantIdentificationStrategy("tenant");
    var builder = new ContainerBuilder();
    builder.RegisterType<BaseDependency>().As<IDependency>();

    // If you have tenant-specific controllers in the same assembly as the
    // application, you should register controllers individually.
    builder.RegisterType<HomeController>();

    // Create the multitenant container and the tenant overrides.
    var mtc = new MultitenantContainer(tenantIdStrategy, builder.Build());
    mtc.ConfigureTenant("CompanyA",
       b =>
        {
          b.RegisterType<Tenant1Dependency>().As<IDependency>().InstancePerDependency();
          b.RegisterType<Tenant1Controller>().As<HomeController>();
        });

If this is the only instance where you need to differentiate the tenants and do not want to Ioc at this point you could create the factory like

static class LogFactory
{
    public static ILog GetLogger()
    {
        var requestUri = HttpContext.Current.Request.Url.AbsoluteUri;
        switch (requestUri)
        {
            case "companyA.domain.com":
                return LogManager.GetLogger("CompanyA");
            case "companyB.domain.com":
                return LogManager.GetLogger("CompanyB");
            default:
                return LogManager.GetLogger("default");
        }
    }
}

now use the factory instead of directly using Logmanager

 ILog logger = LogFactory.GetLogger();


来源:https://stackoverflow.com/questions/23746946/multi-tenant-logging-in-asp-net-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!