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.
| Author |
Messages |
|
omatase
 |
| 04/08/2009 4:30 PM |
|
Ross Hinkley wrote: > > You can specify a threshold value in the appender. That would allow you > to > log messages from a given level and higher. For example, as a child to > the > appender node: > > <threshold value="INFO"> > > would log Info messages, Warning messages, and so on. From there, you can > use Ron's suggestion for controlling what you want to see overall and to > manage multiple appenders. > > Omatase, if you need a more complete example, let me know. > > -Ross > >
I would love an example if you wouldn't mind. It sounds like what you are suggesting might not work in my situation though. I want to make these appenders available for any logging level. 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 messages to the same appender.
Ross Hinkley wrote: > > If I understand your question, Omatase, it sounds like you want to log one > log level per file, maybe? I'm not certain you can specify an upper > threshold to an appender. I've never used log4net in such a fashion. > Does > anyone know if you can set an upper limit on an appender? >
Since this is a service to be used by all applications developed at our company it could potentially be used differently by each application. For my application (The Invoicing example) I want to be able to send FATAL messages to the FileAppender and DEBUG messages to the AdoNetAppender not separated by file.
|
|
|
|
|
omatase
 |
| 04/08/2009 5:33 PM |
|
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.
|
|
|
|
|
omatase
 |
| 04/09/2009 12:09 AM |
|
Daniel Marohn wrote: > > I did some research about this > > I checked the log4net sources. The error is thrown in > AppenderSkelleton (the base class of all appenders). > public void DoAppend(LoggingEvent[] loggingEvents) { > .. > if (m_closed) > { > ErrorHandler.Error("Attempted to append to closed appender named > ["+m_name+"]."); > return; > } > } > > m_closed in only set in > public void Close() > > it reads in code docu: > It is a programming error to append to a closed appender. > > Perhaps this is really a bug in one of the log4net appenders. > > Can you post a simple as possible config, that reproduces the error on > your system? I will try to reproduce this and debug into log4net to > see whats going wrong with the appender. > >
Thanks for your help. Following is a minimalist example of one that fails. The error it causes is "log4net:ERROR [FileAppender] Attempted to append to closed appender named [FileAppender]." and can be seen in the Visual Studio "Output" window.
<log4net> <root> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </root> <logger name="Invoicing"> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </logger> <logger name="Invoicing"> <level value="FATAL" /> <appender-ref ref="FileAppenderFatal" /> </logger> <!-- Setup the root category, add the appenders and set the default level --> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="C:\Log.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="FileAppenderFatal" type="log4net.Appender.FileAppender"> <file value="C:\LogFatal.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net>
You'll see that although the example *may* not be practical, it should be possible. It's not super realistic for someone to want a FileAppender only for FATAL messages although it *could* happen. I replaced the AdoNetAppender with the FileAppenderFatal in the interest of making the smallest possible reproducible configuration.
Interestingly, this configuration does *not* reproduce the error:
<log4net> <root> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </root> <logger name="Invoicing"> <level value="FATAL" /> <appender-ref ref="FileAppenderFatal" /> </logger> <logger name="Invoicing"> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </logger> <!-- Setup the root category, add the appenders and set the default level --> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="C:\Log.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="FileAppenderFatal" type="log4net.Appender.FileAppender"> <file value="C:\LogFatal.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net>
The only difference between the two was I swapped the positioning of the two loggers.
|
|
|
|
|
Ron Grabowski
 |
| 04/12/2009 3:37 PM |
|
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.
|
|
|
|
|
|
| You are not authorized to post a reply. |
|
|
|
ActiveForums 3.7
|