【问题标题】:How to set current time (-zone) for Logging?如何为日志设置当前时间(-zone)?
【发布时间】:2019-08-21 09:53:00
【问题描述】:

我用于记录 Microsoft.Extensions.Logging 和 NLog。日志被写入 Elasticsearch。当我使用 Kibana 查看我的日志时,我的所有日​​志都写在 UT,这意味着与我的本地时间 (-zone) MET 有两个小时不同。我想查看我在 Kibana 的当地时间的日志。 如何调整日志记录的时区?

我添加了 NLog.config:

<nlog autoReload="true" throwExceptions="false"
  internalLogLevel="Info" internalLogFile="NLogError.log"
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <targets>
 <target name="Logfile" xsi:type="File"
  fileName="${basedir}dap.log"
layout="${longdate} ${level} ${callsite} -> ${message} ${exception:format=Message,StackTrace}"
  archiveFileName="${basedir}/archives/log.{#}.log"
  archiveEvery="Day"
  archiveNumbering="Rolling"
  maxArchiveFiles="14"
  keepFileOpen="false"
  />

  <target xsi:type="Network"
        name="CentralLog"
        newLine ="false"
        maxMessageSize="65000"
        connectionCacheSize="5"
        encoding="utf-8"
        keepConnection="false"
        maxQueueSize="100"
        address="tcp://myurl.org:5544"
        onOverflow="Split">
  <layout type="JsonLayout">
    <attribute name="machinename" layout="${machinename}" />
    <attribute name="level" layout="${level:upperCase=true}" />
    <attribute name="processname" layout="${processname}" />
    <attribute name="processid" layout="${processid}" />
    <attribute name="sendertimestamp" layout="${date:universalTime=true:format=yyyy-MM-ddTHH\:mm\:ss.fff}" />
    <attribute name="module" layout="dhp DataPickerApi ${logger}" />
    <attribute name="message" layout="${message}" />
    <attribute name="exception" layout="${exception}" />
    <attribute name="activityId" layout="${activityId}" />
  </layout>
 </target>    
</targets>

 <rules>
  <logger name="*" minlevel="Trace" writeTo="Logfile" />
  <logger name="*" minlevel="Trace" writeTo="CentralLog"/>
 </rules>
</nlog>

【问题讨论】:

  • 您的 NLog 配置中可能有一个选项。请分享您的 NLog 配置
  • 如何将日志事件从 NLog 转发到 Elasticsearch?你用filebeat还是nuget.org/packages/NLog.Targets.ElasticSearch
  • @Julian 我将 NLog.config 添加到我最初的问题中。我尝试使用 这行在节省的时间并没有什么不同。
  • @RolfKristensen 我使用 nuget 包 NLog。 var logger = new LoggerFactory().AddNLog().CreateLogger();

标签: elasticsearch logging timezone nlog


【解决方案1】:

不要使用universalTime=true? 所以不要这样:

<attribute name="sendertimestamp" 
           layout="${date:universalTime=true:format=yyyy-MM-ddTHH\:mm\:ss.fff}" />  

使用这个:

<attribute name="sendertimestamp" 
           layout="${date:format=yyyy-MM-ddTHH\:mm\:ss.fff}" />

【讨论】:

  • 谢谢朱利安!我忽略了这一点。
【解决方案2】:

您可以在 Kibana 网页中设置时区。

设置-->高级---> DateFormat:tz

获取更多信息:https://www.elastic.co/guide/en/kibana/current/advanced-options.html

【讨论】:

  • 其他日志从其他软件进来,并且在正确的时间出现。现在 12:01 的意思是在 12:01 的时间提交日志。但是我的日志是用 10:01 的时间写的。所以我认为当我调整 Kibana 时,比其他日志会有一个错误的时间。此外,它会根据我的浏览器时间进行调整,这应该是正确的。
  • @FrankMehlhop 行业标准是在编写 JSON 时编写 UTC 时间戳。而且 Elasticsearch 真的很喜欢 JSON。 NLog中的IJsonConverter默认转换为UTC(不确定是否可以配置不同)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 2016-03-20
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多