2

I have read through quite a few StackOverflow questions and tried multiple options. I have different databases per tenant and am trying to get a ILog with an appender for a specific database. I have successfully created an AdoNetAppender with the correct db connection string and gotten it into the log4net repository root.

However, when I ask LogManager for a new ILog, it gives me one with no appenders. If I look at the ILog instance's parent's root, it has the AdoNetAppender I added. What am I doing wrong?

Hierarchy h = (Hierarchy)LogManager.GetRepository();
var adoAppender = Log4NetAdoAppenderHelper.LoadADONetAppender(service.GetBoxDbConnectionString());
h.Root.AddAppender(adoAppender);
log4net.Config.BasicConfigurator.Configure(h);

//Neither of these work to get an appender on the ILog (_logger1 or _logger2)
var _logger1 = log4net.LogManager.GetLogger("log4net-default-repository", "AdoNetAppender");
var _logger2 = log4net.LogManager.GetRepository().GetLogger(typeof(AdoNetAppender).FullName);

Some of the StackOverflow Q/As I've looked at extensively and tried code from are: log4net - getting appenders specific to only one logger

How to configure log4net programmatically from scratch (no config)

Enable file logging for log4net from code instead of from configuration

Configure log4net RollingFileAppender in code rolling once per application start

I'm not sure why LogManager doesn't add the configured AdoNetAppender to the ILog object it gives me and am having trouble finding more on that functionality.

JakeJ
  • 2,361
  • 5
  • 23
  • 35
  • This link was a lot of help when making the method (2nd line above) to make the appender: https://csharp.hotexamples.com/examples/log4net.Appender/AdoNetAppender/-/php-adonetappender-class-examples.html – JakeJ Apr 25 '19 at 17:53
  • OK, was able to add the appender to the logger: Might not need any of the root level / hierarchy stuff: var myLogger = LogManager.GetLogger("whatever name you want"); Logger l = (Logger)myLogger.Logger; l.AddAppender(adoAppender); – JakeJ Apr 25 '19 at 21:23
  • Then to see how healthy your appender is / what is actually happening behind the scenes: https://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems – JakeJ Apr 25 '19 at 21:24

0 Answers0