Tuesday, May 21, 2013
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Log4Net API to get appender config properties
Prev Next
You are not authorized to post a reply.

Author Messages
Dan Johnston

12/04/2004 1:45 AM  

Is it possible to access  appender properties via the log4Net API.  For instance, I am interested in discovering the name of a rollingfileAppender logfile name and path, but because it is dynamically created with respect to time, parsing the log4net config file proves futile.

It would appear that I should gain access to the Appenders Collection object, but am uncertain as to how I go about doing this.  Any help on this subject would be greatly appreciated.

Thanks,
dan
Andrew Elmhorst

12/04/2004 1:56 AM  

Sure, here’s a quick code snippet . .

 

This will get you the appenders that have been appended to the root of the hierarchy.

 

IAppender fileApp = null;

IAppender consoleApp = null;

log4net.Repository.Hierarchy.Hierarchy hierarchy = LogManager.GetLoggerRepository() as log4net.Repository.Hierarchy.Hierarchy;

foreach( IAppender app in hierarchy.Root.Appenders )

{

            if (app is ConsoleAppender)

                        consoleApp = app;

            else if (app is RollingFileAppender)

            {

                        fileApp = app;

                        //Update the appender's schedule

                        RollingFileAppender ap = ( RollingFileAppender )app;

                        ap.DatePattern = sCfg.LogFileRollSchedule ;

            }

}

 

Nicko Cadell

12/08/2004 2:23 PM  

You can get all the current appenders by using a method like:

public static log4net.Appender.IAppender[] GetAllAppenders()
{
  ArrayList appenders = new ArrayList();

  log4net.Repository.Hierarchy.Hierarchy h =
(log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepo
sitory();
  appenders.AddRange(h.Root.Appenders);

  foreach(log4net.Repository.Hierarchy.Logger logger in
h.GetCurrentLoggers())
  {
    appenders.AddRange(logger.Appenders);
  }

  return
(log4net.Appender.IAppender[])appenders.ToArray(typeof(log4net.Appender.
IAppender));
}

Nicko

Oliver Sturm

12/08/2004 2:35 PM  

Hey Nicko,

you may have noticed that you answered my recent question ("Finding out
whether a specific appender is attached") with this, too (although I
haven't tried it yet). Just two comments inline below:

Nicko Cadell

12/08/2004 4:03 PM  

Oliver,

> >  log4net.Repository.Hierarchy.Hierarchy h =
> >(log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.Ge
> tLoggerRepository();
> > 
> >
> Why do you assume that this ILoggerRepository is really a Hierarchy?

Currently the only implementation of the ILoggerRepository interface is
Hierarchy. Other implementations may have been developed by you or 3rd
party developers. The only way to get to the root logger is to cast to
the Hierarchy type and use the Root property.

As with all sample code the error checking is left as an exercise for
the reader.


>
> >  appenders.AddRange(h.Root.Appenders);
> >
> >  foreach(log4net.Repository.Hierarchy.Logger logger in
> >h.GetCurrentLoggers())
> > 
> >
> Why do you assume that this ILog is really a Logger?

The Hierarchy.GetCurrentLoggers method can only return
log4net.Repository.Hierarchy.Logger instances therefore this should be a
safe thing to do. A more cautious approach would be to get the results
as an ILogger array (the return type from Hierarchy.GetCurrentLoggers)
and then test if these can be cast to IAppenderAttachable and then use
the IAppenderAttachable.Appenders property to retrieve the appender.


> I usually tend to be quite careful with libraries written by
> other people when interfaces are in use. I assume that
> interfaces have been used for a reason, that reason being
> that it's not safe to assume that the actual object is always
> of some specific class type. Why else would I be using
> interfaces? In log4net, have interfaces been used for some
> other reason so it's safe to make assumptions about the
> classes implementing them?

Interfaces have been used to allow alternative implementations to be
substituted. At the current time there are no alternative
implementations of ILoggerRepostiroy and ILogger in the log4net source,
however you could supply your own alternative implementation, which
would break the sample.

I have added a GetAppenders method to the ILoggerRepostiroy interface in
CVS. This means that it will be the implementers responsibility to
locate and return the appenders in a robust way. This should solve the
issues you raised in future, however at the moment you need to do
something like the sample, hopefully you will find it useful.

Cheers,
Nicko

------------
Nicko Cadell
log4net development
http://logging.apache.org/log4net

Oliver Sturm

12/08/2004 5:58 PM  

Hey Nicko,

Nicko Cadell wrote:

>Interfaces have been used to allow alternative implementations to be
>substituted. At the current time there are no alternative
>implementations of ILoggerRepostiroy and ILogger in the log4net source,
>however you could supply your own alternative implementation, which
>would break the sample.
>
>I have added a GetAppenders method to the ILoggerRepostiroy interface in
>CVS. This means that it will be the implementers responsibility to
>locate and return the appenders in a robust way. This should solve the
>issues you raised in future, however at the moment you need to do
>something like the sample, hopefully you will find it useful.

>
Thanks for your hints and explanations, everything works fine now!

               Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
MSN oliver@sturmnet.org Jabber sturm@amessage.de ICQ 27142619

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Log4Net API to get appender config properties



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement