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 Nicko Cadell <ni...@neoworks.com> on 2005/03/06 21:28:13 UTC

RE: separate log info from a particular method

If you know which logging calls in the code you want to go into separate files then it is best to create separate appenders for each log file as suggested below. The code would select the logger required by calling LogManager.GetLogger with the appropriate name, and that logger would route through to the correct appender.

If you need to configure / reconfigure the selected method at runtime then you will need to write a custom filter to detect the method name, however this may not work in all circumstances as the JIT may optimise away certain method calls (only in Release builds).

Nicko

> -----Original Message-----
> From: Eduard Ralph [mailto:e.ralph@mphasis.biz] 
> Sent: 28 February 2005 17:46
> To: 'Log4NET User'; 'Cheng'
> Subject: AW: separate log info from a particular method
> 
> Hi,
> 
> I won't dig out the manuals to write a config but this should 
> be pretty close to what you would have to write:
> 
> log4net>
> 	<appender name="AAppender" type="Appender.RollingFileAppender">
> 		<file value="A.TXT" />
> 		<appendToFile value="false" />
> 		<!-- A1 uses PatternLayout -->
> 		<layout type="Layout.PatternLayout">
> 			<conversionPattern value="[%d] %p %c %x 
> - %m%n" />
> 		</layout>
> 	</appender>
> 	<appender name="BAppender" type="Appender.RollingFileAppender">
> 		<file value="B.TXT" />
> 		<appendToFile value="false" />
> 		<!-- B1 uses PatternLayout -->
> 		<layout type="Layout.PatternLayout">
> 			<conversionPattern value="[%d] %p %c %x 
> - %m%n" />
> 		</layout>
> 	</appender>
> 	<root>
> 		<level value="ERROR" />
> 		<appender-ref ref="AAppender" />
> 	</root>
> 	<logger name="A">
> 		<appender-ref ref="AAppender" />
> 	</logger>
> 	<logger name="B">
> 		<appender-ref ref="BAppender" />
> 	</logger>
> </log4net>
> 
> Eddie
> -----Ursprüngliche Nachricht-----
> Von: Cheng [mailto:chenghf@gmail.com]
> Gesendet: Montag, 28. Februar 2005 18:15
> An: Log4NET User
> Betreff: separate log info from a particular method
> 
> Hi, 
> 
> I am using RollingFileAppender for my whole application. 
> Suppose the main logfile is A.txt. I want to log all 
> information from a particular method to a separate file say 
> B.txt (these log should not be logged to A.txt). 
> StringMatchFilter filter should be work, however the 
> particular method has to log a particular string in order to 
> use the StringMatchFilter fileter. Is there any other 
> convenient way to archieve this? Ideally just add some config 
> settings.
> thanks
> 
> 

Re: separate log info from a particular method

Posted by Thibaut Barrère <th...@gmail.com>.
Hello,

I have a question which is kind of related to the same topic.

I have a big amount of legacy code with a custom trace system, which
fortunately I'm able to redirect to log4net now.

This legacy trace system has custom names for loggers, such as
"service1","service2" etc, which I cannot determine at compile time.

What would be the cost of having a wrapper which would do (on each log call):
ILog myLogger = LogManager.GetLogger("Legacy_"+logName);
then log accordingly ?

Is there a need to cache the ILog instance or is it not useful at all ?

Is this approach (compared to the classic static readonly ILog = ...
approach) threadsafe from a log4net point of view ?


thanks

Thibaut
http://www.dotnetguru2.org/tbarrere

2005/3/6, Nicko Cadell <ni...@neoworks.com>:
> If you know which logging calls in the code you want to go into separate files then it is best to create separate appenders for each log file as suggested below. The code would select the logger required by calling LogManager.GetLogger with the appropriate name, and that logger would route through to the correct appender.
> 
> If you need to configure / reconfigure the selected method at runtime then you will need to write a custom filter to detect the method name, however this may not work in all circumstances as the JIT may optimise away certain method calls (only in Release builds).
> 
> Nicko
> 
> > -----Original Message-----
> > From: Eduard Ralph [mailto:e.ralph@mphasis.biz]
> > Sent: 28 February 2005 17:46
> > To: 'Log4NET User'; 'Cheng'
> > Subject: AW: separate log info from a particular method
> >
> > Hi,
> >
> > I won't dig out the manuals to write a config but this should
> > be pretty close to what you would have to write:
> >
> > log4net>
> >       <appender name="AAppender" type="Appender.RollingFileAppender">
> >               <file value="A.TXT" />
> >               <appendToFile value="false" />
> >               <!-- A1 uses PatternLayout -->
> >               <layout type="Layout.PatternLayout">
> >                       <conversionPattern value="[%d] %p %c %x
> > - %m%n" />
> >               </layout>
> >       </appender>
> >       <appender name="BAppender" type="Appender.RollingFileAppender">
> >               <file value="B.TXT" />
> >               <appendToFile value="false" />
> >               <!-- B1 uses PatternLayout -->
> >               <layout type="Layout.PatternLayout">
> >                       <conversionPattern value="[%d] %p %c %x
> > - %m%n" />
> >               </layout>
> >       </appender>
> >       <root>
> >               <level value="ERROR" />
> >               <appender-ref ref="AAppender" />
> >       </root>
> >       <logger name="A">
> >               <appender-ref ref="AAppender" />
> >       </logger>
> >       <logger name="B">
> >               <appender-ref ref="BAppender" />
> >       </logger>
> > </log4net>
> >
> > Eddie
> > -----Ursprüngliche Nachricht-----
> > Von: Cheng [mailto:chenghf@gmail.com]
> > Gesendet: Montag, 28. Februar 2005 18:15
> > An: Log4NET User
> > Betreff: separate log info from a particular method
> >
> > Hi,
> >
> > I am using RollingFileAppender for my whole application.
> > Suppose the main logfile is A.txt. I want to log all
> > information from a particular method to a separate file say
> > B.txt (these log should not be logged to A.txt).
> > StringMatchFilter filter should be work, however the
> > particular method has to log a particular string in order to
> > use the StringMatchFilter fileter. Is there any other
> > convenient way to archieve this? Ideally just add some config
> > settings.
> > thanks
> >
> >
>