You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by schlud <lu...@roche.com> on 2008/10/31 14:14:58 UTC

Set & Get MemoryAppender from a named Logger

Following statements are not supportet...

// Getting a Specific Appender from a specific logger
 IAppender myapp =
LogManager.GetLogger("MyNamedLogger1").GetAppender("MyNamedAppender");

// attaching a specific Appender to a specific logger
 LogManager.GetLogger("MyNamedLogger1").SetAppender("MyNamedAppender");


How can I access a desired Appender of a known logger directly, without
iterating over all Appenders and query thier names?


How can I attach a specific Appender to an Specific Logger ?
Thanks in Advance


-- 
View this message in context: http://www.nabble.com/Set---Get-MemoryAppender-from-a-named-Logger-tp20265747p20265747.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


RE: Set & Get MemoryAppender from a named Logger

Posted by schlud <lu...@roche.com>.
Hello Francine 

Thanks, that was what I had searched for!

For setting an appender, i would make something like this:

Hierarchy hier = (Hierarchy)LogManager.GetRepository();
        Logger mylog = (Logger)hier.GetLogger("MyNamedLogger1");
        IAppender memap = new MemoryAppender();
        memap.Name = "blabla";
        mylog.AddAppender(memap);

Have a great time!
bye



Francine Taylor wrote:
> 
> Try this:
> 
> Hierarchy hier = (Hierarchy)LogManager.GetRepository();
> Logger mylog = (Logger)hier.GetLogger("MyNamedLogger1");
> IAppender myap = mylog.GetAppender("MyNamedAppender");
> 
> There is only one problem with this, and that is that if the logger
> doesn't exist, log4net will create it.  myap will still return as null,
> because the newly created logger won't have that appender, but then
> you've got a junk logger in your repository.
> 
> You can prevent that by replacing your GetLogger() call with a looping
> search:
> 
> Hierarchy hier = (Hierarchy)LogManager.GetRepository();
> foreach (ILogger l in hier.GetCurrentLoggers()) {
>     if (l.Name.Equals(loggerName)) {
>         IAppender myap = l.GetAppender("MyNamedAppender");
>         if (myap != null) {
>             // do stuff
>         }
>     }
> }
> 
> ...but then you're back to looping.
> 
> -----Original Message-----
> From: schlud [mailto:lukas.schluechter@roche.com]
> Sent: Friday, October 31, 2008 6:15 AM
> To: log4net-user@logging.apache.org
> Subject: Set & Get MemoryAppender from a named Logger
> 
> 
> Following statements are not supportet...
> 
> // Getting a Specific Appender from a specific logger
>  IAppender myapp =
> LogManager.GetLogger("MyNamedLogger1").GetAppender("MyNamedAppender");
> 
> // attaching a specific Appender to a specific logger
>  LogManager.GetLogger("MyNamedLogger1").SetAppender("MyNamedAppender");
> 
> 
> How can I access a desired Appender of a known logger directly, without
> iterating over all Appenders and query thier names?
> 
> 
> How can I attach a specific Appender to an Specific Logger ?
> Thanks in Advance
> 
> 
> --
> View this message in context:
> http://www.nabble.com/Set---Get-MemoryAppender-from-a-named-Logger-tp202
> 65747p20265747.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
> 
> ==========================================
> NOTICE: The contents of this e-mail message and any attachments are
> intended solely for the addressee(s) named in this message. This
> communication is intended to be and to remain confidential. If you are not
> the intended recipient of this message, or if this message has been
> addressed to you in error, please immediately alert the sender by reply
> e-mail and then delete this message and its attachments. Do not deliver,
> distribute or copy this message and/or any attachments and if you are not
> the intended recipient, do not disclose the contents or take any action in
> reliance upon the information contained in this communication or any
> attachments.
> Thank you
> 
> 

-- 
View this message in context: http://www.nabble.com/Set---Get-MemoryAppender-from-a-named-Logger-tp20265747p20322232.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


RE: Set & Get MemoryAppender from a named Logger

Posted by Francine Taylor <Fr...@genesis-fs.com>.
Try this:

Hierarchy hier = (Hierarchy)LogManager.GetRepository();
Logger mylog = (Logger)hier.GetLogger("MyNamedLogger1");
IAppender myap = mylog.GetAppender("MyNamedAppender");

There is only one problem with this, and that is that if the logger
doesn't exist, log4net will create it.  myap will still return as null,
because the newly created logger won't have that appender, but then
you've got a junk logger in your repository.

You can prevent that by replacing your GetLogger() call with a looping
search:

Hierarchy hier = (Hierarchy)LogManager.GetRepository();
foreach (ILogger l in hier.GetCurrentLoggers()) {
    if (l.Name.Equals(loggerName)) {
        IAppender myap = l.GetAppender("MyNamedAppender");
        if (myap != null) {
            // do stuff
        }
    }
}

...but then you're back to looping.

-----Original Message-----
From: schlud [mailto:lukas.schluechter@roche.com] 
Sent: Friday, October 31, 2008 6:15 AM
To: log4net-user@logging.apache.org
Subject: Set & Get MemoryAppender from a named Logger


Following statements are not supportet...

// Getting a Specific Appender from a specific logger
 IAppender myapp =
LogManager.GetLogger("MyNamedLogger1").GetAppender("MyNamedAppender");

// attaching a specific Appender to a specific logger
 LogManager.GetLogger("MyNamedLogger1").SetAppender("MyNamedAppender");


How can I access a desired Appender of a known logger directly, without
iterating over all Appenders and query thier names?


How can I attach a specific Appender to an Specific Logger ?
Thanks in Advance


-- 
View this message in context:
http://www.nabble.com/Set---Get-MemoryAppender-from-a-named-Logger-tp202
65747p20265747.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


==========================================
NOTICE: The contents of this e-mail message and any attachments are intended solely for the addressee(s) named in this message. This communication is intended to be and to remain confidential. If you are not the intended recipient of this message, or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and its attachments. Do not deliver, distribute or copy this message and/or any attachments and if you are not the intended recipient, do not disclose the contents or take any action in reliance upon the information contained in this communication or any attachments.
Thank you