In my ‘Getting Started with log4net‘ post, I described how I was able to quickly and easily get log4net working using the FileAppender and the ConsoleAppender.
I’m actually using the AdoNetAppender in some applications I’m currently working on (I’m actually using the RollingFileAppender, ConsoleAppender and the AdoNetAppender, but more on that later).
The sample for using the AdoNetAppender given in the log4net docs
Perfect, you just saved me hours of work. Thank you for posting this.
Hi, I have a question, but I don’t know if this is the correct area to place it:
I have an ASP.Net application and I store many Session variables, so I’ve created a separate config file for log4net (log4net.config) because if any change is made to the web.config file all my Session variables are blown away. I have configured it with the following:
Global.asax.Application_Start:
MDC.Set(“host”, System.Net.Dns.GetHostName());
log4net.Repository.Hierarchy.Hierarchy hier =
log4net.LogManager.GetLoggerRepository() as log4net.Repository.Hierarchy.Hierarchy;
if(hier != null)
{
// get ADONetAppender
log4net.Appender.ADONetAppender adoAppender =
(log4net.Appender.ADONetAppender)hier.GetLogger(
“AppName”, hier.LoggerFactory).GetAppender(“ADONetAppender”);
if(adoAppender != null)
{
adoAppender.ConnectionString =
System.Configuration.ConfigurationSettings.AppSettings["Logger.ConnectionString"];
adoAppender.ActivateOptions();
}
}
log4net.Config.DOMConfigurator.Configure();
Web.config:
AssemblyInfo.cs
[assembly: log4net.Config.DOMConfigurator(ConfigFile="log4net.config", Watch=true)]
Log4net.config
If I have my root level set to DEBUG, I can change it to OFF and log4net stops logging (as expected), but if I decide to change back to DEBUG it doesn’t restart logging. Is there something that I have configured incorrectly? I have tried add and set the Threshold in the log4net tag, the ADONetAppender, as well as under the root tag, to No avail. I have also tried to call ConfigureAndWatch in the Application_Start, but that didn’t work either. Am I missing something?
Thanks for any kind of assistance,
Jason Goldsmith
jgoldsmith@psmic.com
Re: the question from Jason…
Well, it seems like the “watch=true” in your AssemblyInfo.cs should do the trick, so I’m not sure why the change isn’t taking affect. If you restart the application, the change does take affect, correct?
Is the log4net.config in the same directory as your assemblies, or with the web.config file?
Just started using log4net with the AdoNetAppender. Seems that everything is working except that when I get the logger (using log4net.Config.ConfigurationManager.GetLogger(typeof(className))), all the enabled properties are set to false. I am confused at this point. any ideas? I’m using the 2.0 framework fyi…
hi
please provide me ur code using AdoNetAppender.
It would be of gr8 help 2 me.
Thanks,
sudi
Hey man! That’s all about configuring to prepare for storing log in the database. How to use the setting from the class file? Any example please. Thank you.