Obtain the root logger programmatically in log4net

If you use log4net, you could have the need to configure the loggers programamtically. The problem arise when you want access to the root logger, since the documentation does not explain well how to obtain it.
So, here’s a snippet that show how to do it. Don’t forget to imort the log4net namespaces.

  1. using log4net;
  2. using log4net.Core;
  3. using log4net.Appender;
  4. using log4net.Layout;
  5. using log4net.Repository.Hierarchy;
  6. using log4net.Config;
  1. Hierarchy h = (Hierarchy)log4net.LogManager.GetRepository();
  2. Logger rootLogger = h.Root;

One thought on “Obtain the root logger programmatically in log4net

  1. Many usefull information along with motivation, both of which every one of us have to have, appreciate that.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>