8000 Time Source · NLog/NLog Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Rolf Kristensen edited this page Oct 19, 2024 · 16 revisions

NLog outputs its timestamps in local time by default, as this is expected by most first time users. The conversion from machine time to regional local time has an overhead, so NLog uses a cached time-source (FastLocal) by default, which only refreshes every 15 msec (Follows Environment.TickCount)

NLog has the ability to change the time source, that provides timestamping of LogEvents. This can be done from code, and also via XML-file.

The following sections were based and/or provided by Robert Važan’s blog:

Configure Via XML

The time source can be configured via XML by setting the time element with the XML name. The provided time sources don’t include “TimeSource” in their XML names.

<nlog>
    <time type="AccurateUtc" />
    <!-- Rest of Configuration -->
</nlog>

Configure Via Runtime Configuration

The time source can be configured by inserting the following before any log messages are printed:

NLog.Time.TimeSource.Current = new NLog.Time.AccurateUtcTimeSource();

Provided Time Sources

The time source implementations that are included with NLog, either provides local-time or UTC-time. Where NLog uses FastLocal by default with low accuracy, but high performance:

XML Configuration Is Default? Runtime Configuration Time Accuracy Speed
FastLocal Yes FastLocalTimeSource local 16ms very fast
FastUTC No FastUtcTimeSource UTC 16ms very fast
AccurateLocal No AccurateLocalTimeSource local 1ms slow
AccurateUTC No AccurateUtcTimeSource UTC 1ms fast

Performance benchmarks for time source implementations (used BenchmarkDotNet):

Method 32-bit Mean 64-bit Mean
FastLocal 5.205 ns 3.546 ns
FastUtc 5.202 ns 3.541 ns
AccurateLocal 190.481 ns 85.927 ns
AccurateUtc 137.258 ns 66.451 ns

The accuracy relates directly to Windows and how it balances accuracy, speed, and efficiency (i.e. battery life). By default, Windows is set to 16ms of accuracy, but can be changed via the API calls of timeBeginPeriod and timeEndPeriod.

Custom Time Source

Before implementing your own time source, see the provided time sources first. If none of those fit your needs, this section will guide you through the process.

Create a class that inherits from TimeSource and implement the inherited property getter Time and method FromSystemTime.

  • The property getter Time provides the current time instance.

  • The method FromSystemTime converts the system time to the same form as if the time source provided it.

[TimeSource("CustomTimeZone")]
public class CustomTimeZoneTimeSource : TimeSource
{
    string ZoneName;
    TimeZoneInfo ZoneInfo;

    [Required]
    public string Zone
    {
        get { return ZoneName; }
        set
        {
            ZoneName = value;
            ZoneInfo
                = TimeZoneInfo.FindSystemTimeZoneById(value);
        }
    }
    
    public override DateTime Time
    {
        get
        {
            return TimeZoneInfo.ConvertTimeFromUtc(
                DateTime.UtcNow, ZoneInfo);
        }
    }
    
    public override DateTime FromSystemTime(DateTime systemTime)
    {
        return TimeZoneInfo.ConvertTimeFromUtc(systemTime, ZoneInfo);
    }
}

In addition, add an attribute to the top that provides the name when specifying the type via XML.

Your custom time source can be loaded by NLog just like any other NLog extension. See how to use the custom target / layout renderer.

The Runtime Configuration would look like this:

TimeSource.Current = new CustomTimeZoneTimeSource()
{
    Zone = "Central Standard Time"
}

The XML Configuration would look like this:

<nlog>
    <time type="CustomTimeZone" zone="Central Standard Time" />
</nlog>
Clone this wiki locally
0