Custom date format with NLog

巧了我就是萌 提交于 2021-01-27 14:02:10

问题


It appears to me as tho the in-built date formats in NLog do not include the timezone correctly. We need to log UTC time WITH the trailing Z so that Splunk knows what the local time is, eg:

{date:universalTime=true:format=yyyy-MM-dd HH:mm:ss.ffffZ}

This produces the correct dates we need.

Rather than inserting this in all our configs across multiple apps, I'd prefer to define a variable that will do this, eg:

{ourdate}

I've had a hack around but I cant figure out how to do this. Is it possible?

Thanks

PS. {longdate} does include the timezone but it drops the milliseconds.


回答1:


The easiest thing for you to do is probably to just bite bullet and use the regular NLog DateLayoutRenderer and specify the configuration values in every config file. If you'd rather keep your config files simple, you could write your own date LayoutRenderer that generates the date in a specific format.

Here is a rough implementation (not tested). It will always log the date in the format that you specified above. I based it on NLog's DateLayoutRenderer, the source of which you can find here:

https://github.com/NLog/NLog/tree/master/src/NLog/LayoutRenderers

You don't really need more options, because you can easily achieve the format you want using the built in DateLayoutRenderer. This implementation just saves you some effort in your NLog.config file(s).

namespace NLog.LayoutRenderers
{
    using System.ComponentModel;
    using System.Text;
    using NLog.Config;

    /// <summary>
    /// Current date and time.
    /// </summary>
    [LayoutRenderer("utczdate")]
    [ThreadAgnostic]
    public class UTCZDateLayoutRenderer : LayoutRenderer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UTCZDateLayoutRenderer" /> class.
        /// </summary>
        public UTCZDateLayoutRenderer()
        {
        }

        /// <summary>
        /// Renders the current date and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            var ts = logEvent.TimeStamp;
            builder.Append(ts.ToString("yyyy-MM-dd HH:mm:ss.ffffZ", CultureInfo.InvariantCulture));
        }
    }
}

You can use this in your NLog.config file like this:

{utczdate}

Good luck!




回答2:


Use ${date:universalTime=true:format=o}

Format 2015-11-06T14:24:52.4025753Z ISO 8601

You are wanting the Round-Trip ("O","o") Format Specifier

The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information and emits a result string that complies with ISO 8601.



来源:https://stackoverflow.com/questions/29222410/custom-date-format-with-nlog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!