NLog将UTC转换为Azure中的本地时区?



我刚开始使用Azure,我正在使用NLog日志到Azure Blob存储。我终于能够得到工作,但现在我看到日志文件正在放置UTC的时间戳。是否有一种方法,我可以格式化我的配置文件,以转换时间戳到美国中央时区?

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
internalLogFile="C:tempfallback-log.txt"
throwConfigExceptions="true">

<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Extensions.AzureBlobStorage" />
</extensions>

<targets async="true">
<target
xsi:type="AzureBlobStorage"
name="azure"
layout="${longdate:universalTime=true} ${level:uppercase=true} - ${logger}: ${message} ${exception:format=tostring}"
connectionString="your-connection-string-goes-here"
container="logs"
blobName="${date:universalTime=true:format=yyyy-MM-dd}.log" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="azure" />
</rules>
</nlog>

很可能默认是容器的UTC本地时间;)

所以在容器中更改时区可能确实是一个好方法。

如果你真的不喜欢这样做,你可以创建一个自定义布局渲染器:

  1. 尽快注册
    // Usage logEventInfo, ${date_cst} - convert UTC to CST
    var timezone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    NLog.LogManager.Setup().SetupExtensions(s =>
    s.RegisterLayoutRenderer("date_cst", (logEvent) => TimeZoneInfo.ConvertTime(logEvent.TimeStamp, timezone).ToLongDateString())
    ); 
    
  2. 在配置中使用${date_cst}

参见NLog -如何编写自定义布局渲染器

最新更新