简体   繁体   中英

Logging messages to different databases

I'm very new to 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

<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. Are they instantited once per logger instance creating or are they instantiated once for every single call to ILog.Info(obj) method call?

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.

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. 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 . The existing AdoNetAppender is not sealed so you can inherit from this and override it. 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. Thank you @stuartd for the pointer on ResolveConnectionString.
  2. You can create your own custom version of the AdoNetAppender that has a dynamic database connection string. 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?).

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).

Thanks Igor for suggesting few ways to resolve connection string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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