在ASP.NET MVC应用程序的多租户记录 [英] Multi-tenant logging in ASP.NET MVC application

查看:125
本文介绍了在ASP.NET MVC应用程序的多租户记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC 5多租户应用程序,可以接受来自多个公司客户端登录。目前,我使用log4net的记录到文件,但将所有从所有公司记录在一个文件中。理想情况下,我想分离出的日志文件,以便为每个公司日志驻留在自己的文件夹中。

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.

我从<一见href=\"http://stackoverflow.com/questions/308436/log4net-programmatically-specify-multiple-loggers-with-multiple-file-appenders\">this 的问题,我可以编程方式创建额外的追加程序,然后就可以登录到不同的日志文件。但是,这将意味着一类的每一个实例,我会不断的打电话

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.

感谢。

推荐答案

你在你的应用程序中使用IoC容器?我能解决使用AutoFac MultitenantContainer类似的问题。你需要要遵循的步骤

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. 定义你会使用识别每个租户的策略。

  2. 注册与Autofac容器通用采集介面

  3. 为每个租户登记的具体记录。

您code可能看起来像(摘自 Autofac维基

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();

这篇关于在ASP.NET MVC应用程序的多租户记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆