简体   繁体   English

将消息记录到不同的数据库

[英]Logging messages to different databases

I'm very new to Log4Net. 我对Log4Net很新。 Please help me understand some concepts. 请帮我理解一些概念。

In my application, I have a single logger. 在我的应用程序中,我有一个记录器。 I would like to log messages to different databases based on some conditional logic. 我想根据一些条件逻辑将消息记录到不同的数据库。

In app config file, I have a logger routing to an AdonetAppender 在app配置文件中,我有一个记录器路由到AdonetAppender

<log4net>
<appender name="SQLAppender" type="log4net.Appender.AdoNetAppender">     
   <connectionType value="" />
   <connectionString value="" />
</appender
<logger name="AuditLogger">
  <level value="ALL" />
  <appender-ref ref="SQLAppender"/>
</logger>
</log4net>

Is there a way to set the connection string dynamically via code? 有没有办法通过代码动态设置连接字符串?

My logger class is designed to be a singleton. 我的记录器类设计为单例。 I would like to know how the appenders are instantiated. 我想知道appender是如何实例化的。 Are they instantited once per logger instance creating or are they instantiated once for every single call to ILog.Info(obj) method call? 它们是否每个logger实例创建一次,或者每次调用ILog.Info(obj)方法调用时都会实例化一次?

Thanks. 谢谢。

Every client uses the same code base. 每个客户端使用相同的代码库。 The only thing that changes is the connection string value based on the client. 唯一改变的是基于客户端的连接字符串值。

To me this is not 100% clear, I read this as 2 possible scenarios and so 2 possible solutions based on your configuration. 对我而言,这并非100%明确,我将其视为2种可能的情况,因此根据您的配置提供了2种可能的解决方案。

Multiple deployments 多个部署

No custom code is really necessary because your deployment should know what the database is that it is working on/with. 实际上没有必要使用自定义代码,因为您的部署应该知道它正在处理/使用的数据库。 Based on that you need only configure your AdoNetAppender once on initialization. 基于此,您只需在初始化时配置一次AdoNetAppender。 I assume that it is not this simple because you mention you got this to work in your comments. 我认为这不是那么简单,因为你提到你在评论中有这个工作。

Multitenant Application 多租户申请

You have a single deployment for all your customers and the code figures out the target database based on the origin of the request. 您为所有客户进行了单一部署,代码根据请求的来源计算出目标数据库。

  1. I think the easiest way to accomplish this is to create a custom log4net appender . 我认为最简单的方法是创建一个自定义的log4net appender The existing AdoNetAppender is not sealed so you can inherit from this and override it. 现有的AdoNetAppender没有密封,因此您可以继承并覆盖它。 You then need to override the protected method ResolveConnectionString with your own code where you resolve the connection string based on the execution or request context. 然后,您需要使用自己的代码覆盖受保护的方法ResolveConnectionString ,您可以根据执行或请求上下文解析连接字符串。 Thank you @stuartd for the pointer on ResolveConnectionString. 感谢@stuartd获取ResolveConnectionString上的指针。
  2. You can create your own custom version of the AdoNetAppender that has a dynamic database connection string. 您可以创建自己的AdoNetAppender自定义版本,该版本具有动态数据库连接字符串。 The only other moving part would be assigning it based on the context as you don't mention how you determine this (or what this context even is, maybe a URL if its a web app?). 唯一的其他移动部分是基于上下文分配它,因为你没有提到你如何确定这个(或者这个上下文甚至是什么,如果它是一个web应用程序,可能是一个URL?)。

Once you have created your appender just add it to the .config file (or configure it programmatically if you prefer not to use a config file). 一旦创建了appender,只需将其添加到.config文件中(或者如果您不想使用配置文件, .config编程方式配置它)。

Thanks Igor for suggesting few ways to resolve connection string. 感谢Igor建议解决连接字符串的几种方法。

However, I'm doing something like this. 但是,我正在做这样的事情。

var loggerRepo = LogManager.GetRepository();
if (loggerRepo != null) {
var appender = loggerRepo.GetAppenders().OfType < AdoNetAppender > ().First(a => a.Name == "YourAppenderName");

appender.ConnectionString = ConfigurationManager.ConnectionStrings[""].ConnectionString;
appender.ActivateOptions();
}
_log.Info("LogSOmeth9ng");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM