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: precise time stamp
Date Thu, 12 Nov 2009 22:58:06 GMT
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 <airat.halimov@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.
Mime
View raw message