使用 NLog 自定义日期格式 [英] Custom date format with NLog

查看:255
本文介绍了使用 NLog 自定义日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,NLog 中的内置日期格式没有正确包含时区.我们需要使用尾随 Z 记录 UTC 时间,以便 Splunk 知道本地时间是什么,例如:

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}

{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:

{我们的日期}

我遇到了一个黑客,但我不知道如何做到这一点.可能吗?

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

谢谢

附注.{longdate} 确实包含时区,但会降低毫秒数.

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

推荐答案

对您来说,最简单的方法可能就是硬着头皮使用常规的 NLog DateLayoutRenderer 并在每个配置文件中指定配置值.如果您想让配置文件保持简单,您可以编写自己的日期 LayoutRenderer,以特定格式生成日期.

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.

这是一个粗略的实现(未测试).它将始终以您在上面指定的格式记录日期.我基于 NLog 的 DateLayoutRenderer,你可以在这里找到它的来源:

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

您实际上并不需要更多选项,因为您可以使用内置的 DateLayoutRenderer 轻松实现您想要的格式.此实现只是为您在 NLog.config 文件中节省了一些精力.

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));
        }
    }
}

您可以像这样在 NLog.config 文件中使用它:

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

{utczdate}

祝你好运!

这篇关于使用 NLog 自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆