logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Error destroying my productivity Please Help!: log4net:ERROR [] Attempted to append to closed appender named []
Date Sun, 12 Apr 2009 13:37:29 GMT
Ross' writeup is correct.

This is not the correct way to define a logger:

<logger name="Invoicing">
  <level value="DEBUG" />
  <appender-ref ref="AdoNetAppender" />
</logger>
<!-- WRONG -->
<logger name="Invoicing">
  <level value="FATAL" />
  <appender-ref ref="FileAppender" />
</logger>

That's the not the documented way to define loggers so I'd say that behavior is undefined...in
this case it throws an exception that can probably be guarded against fairly easily.



________________________________
From: Ross Hinkley <rosshinkley@gmail.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Wednesday, April 8, 2009 12:07:37 PM
Subject: Re: Error destroying my productivity Please Help!: log4net:ERROR []  Attempted to
append to closed appender named []

Omatase,

What you want would be something like the following (I'm guessing):

<log4net debug="false" threshold="ALL" >
    <appender name="WarningAppender" type="log4net.Appender.ConsoleAppender">
        <threshold value="WARN"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="[WarningAppender] - %message%newline" />
        </layout>
    </appender>
    <appender name="InfoAppender" type="log4net.Appender.ConsoleAppender">
        <threshold value="INFO"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="[InfoAppender] - %message%newline" />
        </layout>
    </appender>

    <root>
        <appender-ref ref="WarningAppender" />
        <appender-ref ref="InfoAppender"/>
    </root>
</log4net>

This way, if you log to the same logger, you'll get two separate messages in (potentially)
two separate places.  

It sounds like you want to tackle a fairly common scenario: When your application encounters
a fatal error, you want to receive a notification about it.  You could set up your SNMP appender
to have a threshold of FATAL.  You would also receive that error in the other appenders you
have set up.  Typically, I would think you wouldn't care if you get the fatal error logged
multiple times, especially so you could figure out (from the DEBUG log, for example) what
your application was trying to do when the error occurred.

Does that make sense?

-Ross



On Wed, Apr 8, 2009 at 10:33 AM, omatase <Omatase@gmail.com> wrote:




Daniel Marohn wrote:
>
> Hi!
>
> ...
> Someone else might have an application that they would like to send
> DEBUG messages to the FileAppender
> even though my application may only send FATAL
> ...
>
> this is, why you have different logger.
> from your first post:
>
> <logger name="Invoicing">
> ...
> </logger>
>
> <logger name="Invoicing">
> ...
> </logger>
>
>

Yes, that *is* why I am doing that



Daniel Marohn wrote:
>
>
> this makes no sense. You configure the logger 'Invoicing' and later
> you configure the SAME logger with different properties. How do you
> want to access these 'two' loggers from your code?
> LogManger.GetLogger("Invoicing, but please first version") ? ;-)
>

log4net is intelligent enough to handle this.

When you call :

log4net.LogManager.GetLogger(loggerName).Debug(message, exception);
or
log4net.LogManager.GetLogger(loggerName).Error(message, exception);

It will use the logger defined with value="DEBUG"

But, when you call

log4net.LogManager.GetLogger(loggerName).Fatal(message, exception);

log4net will use the logger defined with value="FATAL"

This allows me to use the same loggerName throughout my application, and
have FATAL messages logged in a different manner. FATAL messages are more
urgent and require immediate attention for this I will be using an
SnmpAppender that will send the messages directly to our critical problem
monitoring system.



Daniel Marohn wrote:
>
> You can do this:
>
>   <appender name="consoleTestAppender"
> type="log4net.Appender.ConsoleAppender" >
>     <layout type="log4net.Layout.PatternLayout">
>       <conversionPattern value="%date %-5level %logger - %message%newline"
> />
>     </layout>
>   </appender>
>
> <logger name="Invoicing.Application1">
>  <level value="ERROR" />
>  <appender-ref ref="consoleTestAppender" />
> </logger>
>
> <logger name="Invoicing.Application2">
>  <level value="WARN" />
>  <appender-ref ref="consoleTestAppender" />
> </logger>
>
> now you have two different loggers  (one for each app), using the same
> appender. And you can set the Level per Logger.
>
>

The invoicing application is a single application. If I were to define
multiple loggers with different names I might do something like this
instead:

<logger name="Invoicing.Logger1">
 <level value="FATAL" />

 <appender-ref ref="consoleTestAppender" />
</logger>

<logger name="Invoicing.Logger2">

 <level value="WARN" />
 <appender-ref ref="consoleTestAppender" />
</logger>

The problem here is I am trying to send my critical errors to Snmp, so in my
code I would have to remember the logger name I am using for critical errors
(in this case Invoicing.Logger1). It is much simpler to just have to
remember to call ".Fatal" when I have a critical error and not have to
remember the loggername that was meant to handle fatal errors.

--
View this message in context: http://www.nabble.com/Error-destroying-my-productivity-Please-Help%21%3A-log4net%3AERROR----Attempted-to-append-to-closed-appender-named----tp22939427p22953058.html

Sent from the Log4net - Users mailing list archive at Nabble.com.
Mime
View raw message