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 Ayrat Khalimov <ai...@gmail.com> on 2009/11/12 13:49:13 UTC

precise time stamp

Hi, there

I need to log events with precise time stamps (more precise than 15 ms
System.DateTime).

But it seems to be impossible to do it with log4net.
It is impossible to change default timer neither to implement special
appender that will set precise value to TimeStamp.
The only solution I see is to write precise time to the event message body.

Any ideas, colleagues?

-- 
Sincerely,
Ayrat Khalimov.

RE: precise time stamp

Posted by Roy Chastain <Ro...@roychastain.org>.
I have never thought about the granularity of the time source being
used, but I have log entries as often as 1 ms apart.

Now that I think about this, why do you believe that your are limited to
15ms resolution.  The documentation for System.DateTime says it uses 100
nanosecond ticks.

I simply place the time stamp in the log entry with the following
conversion pattern
<param name="ConversionPattern" value="%d{dd HH:mm:ss.fff} [%4t]
%P{instance}::%M - %m%n" />

This yields the following entries
20 10:28:12.214 [ServiceThread] GSNT(17)::.ctor - Constructed -
Id/Base/Count/Type:
f4119ba3-ce74-4729-bac9-ccfe5a9b300e/o2203/6/NumericOnly
20 10:28:12.215 [ServiceThread] FCICMSD::.ctor - Constructed OpenId -
Name/VDest/Pool: FCICMSD/FCICMS/GSNT(17)

Since this is a service, I put the day in and then the time.

----------------------------------------------------------------------
Roy Chastain




-----Original Message-----
From: Ayrat Khalimov [mailto:airat.halimov@gmail.com] 
Sent: Thursday, November 12, 2009 07:49
To: log4net-user@logging.apache.org
Subject: precise time stamp

Hi, there

I need to log events with precise time stamps (more precise than 15 ms
System.DateTime). 

But it seems to be impossible to do it with log4net. 
It is impossible to change default timer neither to implement special
appender that will set precise value to TimeStamp.
The only solution I see is to write precise time to the event message
body.

Any ideas, colleagues?


-- 
Sincerely, 
Ayrat Khalimov.



Re: precise time stamp

Posted by Ron Grabowski <ro...@yahoo.com>.
Do you want to control how the time is displayed in the log file or fundamentally alter how log4net tracks time?

If you want to use a custom DateTime.Now to record when the event created you could start up by defining your own IDateTime:

// untested
public interface IDateTime
{
    DateTime Now { get; }
}

then make it easy to get an instance of IDateTime:

// untested
public class DateTimeProvider : IDateTime
{
    public static readonly DateTimeProvider DateTimeNow = new DateTimeProvider(() => DateTime.Now);
    public static readonly DateTimeProvider DateTimeUtcNow = new DateTimeProvider(() => DateTime.UtcNow);

    private Func<DateTime> _dateTimeGetter;

    public DateTimeProvider(Func<DateTime> dateTimeGetter)
    {
        _dateTimeGetter = dateTimeGetter;
    }

    public DateTime Now
    {
        get { return _dateTimeGetter(); }
    }

    public void SetDateTimeFunc(Func<DateTime> dateTimeGetter)
    {
        _dateTimeGetter = dateTimeGetter;
    }
}

then you could tweak how the LoggingEventData object is created:

// untested
class DateTimeProviderLogger : LogImpl
{
    private readonly static Type declaringType = typeof(DateTimeProviderLogger);
    
    private readonly IDateTime dateTimeProvider;

    public DateTimeProviderLogger(ILogger logger, IDateTime dateTimeProvider) : base(logger)
    {
        this.dateTimeProvider = dateTimeProvider;
    }

    public override void Debug(object message)
    {
        Logger.Log(createLoggingEvnet(Level.Debug, message));
    }

    private LoggingEvent createLoggingEvnet(Level level, object message)
    {
        return new LoggingEvent(
            declaringType, 
            Logger.Repository,
            createLoggingEventData(level, message));
    }

    private LoggingEventData createLoggingEventData(Level level, object message)
    {
        return new LoggingEventData
                   {
                       Level = level,
                       LoggerName = Logger.Name,
                       Message = message.ToString(),
                       TimeStamp = dateTimeProvider.Now
                   };
    }
}

The calling code might look something like this:

// untested
public static class LogManager2
{
    public static ILog GetLogger<T>()
    {
        return new DateTimeProviderLogger(LogManager.GetLogger(typeof(T)).Logger, DateTimeProvider.DateTimeNow);
    }
}

// untested
class Program
{
    public static readonly ILog log = LogManager2.GetLogger<Program>();
}




________________________________
From: Ayrat Khalimov <ai...@gmail.com>
To: log4net-user@logging.apache.org
Sent: Thu, November 12, 2009 7:49:13 AM
Subject: precise time stamp


Hi, there

I need to log events with precise time stamps (more precise than 15 ms System.DateTime). 

But it seems to be impossible to do it with log4net. 
It is impossible to change default timer neither to implement special appender that will set precise value to TimeStamp.
The only solution I see is to write precise time to the event message body.

Any ideas, colleagues?

-- 
Sincerely, 
Ayrat Khalimov.