logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steven Whatmore <Steven.Whatm...@purefacts.com>
Subject RE: Controlling the log message format
Date Wed, 12 May 2010 11:40:52 GMT
Ross,

We have a very similar set of Appenders to the log4net (as expected since it is a port as
far as I know)

	JAVA					.NET

	ConsoleAppender			DebugAppender
	JDBCAppender			ADONetAppender
	DailyRollingFileAppender	RollingFileAppender

In fact I believe that most of the core Appenders included with log4j have a corresponding
counterpart in log4net

The configuration is slightly different depending on what version since the version that we
are currently using in log4j is using the older property file format as opposed to an XML
configuration file (but I believe the newer version of log4j has the XML configuration file
option), and the actual specification for the formats are specified in a different manner

As an example, here is the configuration option for the ConsoleAppender

    Java

    log4j.appender.DebugAppender=org.apache.log4j.ConsoleAppender
    log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.DebugAppender.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c %M - %m\n

    .NET
	
    <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level [%thread] - %message%newline" />
        </layout>
    </appender>

and the root logger

    Java

    log4j.rootLogger=DEBUG, DebugAppender, DailyRollingFileAppender

    .NET

    <root>
        <level value="DEBUG" />
        <appender-ref ref="DebugAppender" />
        <appender-ref ref="RollingFileAppender" />
    </root>

The similarities should be fairly obvious.

Whatty
 
From: Ross Hinkley [mailto:rosshinkley@gmail.com] 
Sent: Tuesday, May 11, 2010 4:27 PM
To: Log4NET User
Subject: Re: Controlling the log message format

Here to help.  We both learned something new. :)

I won't pretend to know much about log4j, but I don't recall it having a DebugAppender counterpart. 
(Is there something like a JDB appender?)

If you're doing a conversion, what appender type are you converting from in log4j?

I realize this semi-digressive, but I have to admit, I'm curious.

-Ross
On Tue, May 11, 2010 at 2:55 PM, Steven Whatmore <Steven.Whatmore@purefacts.com> wrote:
Ross,

Thanks for the help, greatly appreciated.

We are in the process of porting the application from one platform (Java) to .NET and on the
Java platform we have the ability to control all aspects of the log message. Thus I was expecting
to be able to replicate the message format in .NET so that it came out the same way.

Understanding the log4net is a port I am not surprised to see minor differences in the implementations,
but am a little surprise to see the different behaviour between the two different types of
loggers.

Thanks for the insight.

Whatty

From: Ross Hinkley [mailto:rosshinkley@gmail.com]
Sent: Tuesday, May 11, 2010 2:05 PM
To: Log4NET User
Subject: Re: Controlling the log message format

Now, that is interesting.

Apparently, log4net sets the category of the debug message to the logger name.  You can try
it for yourself:

logger.Debug("hello!");
Debug.WriteLine("hello!",logger.Logger.Name);

.... should produce the same output.

This is actually by design, per the documentation:

http://logging.apache.org/log4net/release/sdk/log4net.Appender.DebugAppender.html

So, back to your original question before I totally understood the problem: yes, you can control
this, but you'll have to set up another logger.  You could do something like this:

<logger name="">
            <appender-ref ref="DebugAppender"/>
        </logger>

...and then use

log4net.LogManager.GetLogger(string.Empty);

...to accomplish what you're trying to do, at least for development.  You should not leave
it that way for production.  In fact, I'd recommend naming the logger something meaningful
(although shorter than your namespace) and using that.

-Ross
On Tue, May 11, 2010 at 12:44 PM, Steven Whatmore <Steven.Whatmore@purefacts.com> wrote:
First, thanks for the assistance.

Okay so I took out %logger from the configuration

   <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
       <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%date %-5level [%thread] - %message%newline"
/>
       </layout>
   </appender>
   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
       <file value="c:\app.log" />
       <appendToFile value="true" />
       <rollingStyle value="Size" />
       <maxSizeRollBackups value="10" />
       <maximumFileSize value="100KB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%date %-5level [%thread] %logger - %message%newline"
/>
       </layout>
   </appender>
and this is what I got

       com.pf.util.implementation.AppConfigurationImpl: 2010-05-11 12:50:05,466 DEBUG
[9] - handling sessionStart event

I am grabbing the logger by Type using

       protected static new readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

and my current configuration only has the root logger defined

   <root>
       <level value="DEBUG" />
       <appender-ref ref="DebugAppender" />
       <appender-ref ref="RollingFileAppender" />
   </root>

Interesting enough the entries in my RollingFileAppender are coming out exactly as I expected.

       2010-05-11 13:33:54,150 DEBUG [13] com.purefacts.util.reports.implementation.ReportingApplicationConfigurationImpl
- handling sessionStart event




From: Ross Hinkley [mailto:rosshinkley@gmail.com]
Sent: Tuesday, May 11, 2010 1:30 PM
To: Log4NET User
Subject: Re: Controlling the log message format
Won't %logger contain the logger name?  How are you retrieving the logger from the log manager? 
If you're using it by type, and are using the root logger, I believe the log name defaults
to the namespace of your binary.

-Ross
On Tue, May 11, 2010 at 12:17 PM, Steven Whatmore <Steven.Whatmore@purefacts.com> wrote:
That is what I thought - but no luck with that - I can't remember which class I was looking
at but it sure looks like you can't control that prefix value - which I thought was very strange
 
From: Ross Hinkley [mailto:rosshinkley@gmail.com]
Sent: Tuesday, May 11, 2010 1:07 PM
To: Log4NET User
Subject: Re: Controlling the log message format
 
This should be controllable via the conversionPattern.  Something like the following in your
appender, maybe?

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date %-5level - %message%newline" />
</layout>

-Ross
On Tue, May 11, 2010 at 11:58 AM, Steven Whatmore <Steven.Whatmore@purefacts.com> wrote:
Good morning,

I would like to be able to control the format of the message, removing the initial prefix

As an example if the format of my message is:

       com.pf.util.implementation.AppConfigurationImpl: 2010-05-11 12:50:05,466 DEBUG
[9] com.pf.util.implementation.AppConfigurationImpl - handling sessionStart event

I would like to remove the initial "com.pf.util.implementation.AppConfigurationImpl" part
resulting in:

2010-05-11 12:50:05,466 DEBUG [9] com.pf.util.implementation.AppConfigurationImpl - handling
sessionStart event

I have tried it through configuration but can't seem to control the prefix

I took a quick look through the code and as far as I can tell this is not configurable.

Am I correct?

Whatty
 


Mime
View raw message