【问题标题】:NLog - Variable Update (at runtime)NLog - 变量更新(在运行时)
【发布时间】:2016-10-12 17:44:23
【问题描述】:

尝试在运行时使用 vb.net (asp.net) 应用程序上的 NLog 更新变量,但似乎无法正常工作。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">


<variable name="DebugInfoLayout" value="[${date:format=MM/dd/yyyy hh\:mm\:ss.fff tt}] [${gdc:item=location}]  |  ${level}  |  ${message}" />
  <variable name="InfoLayout" value="[${date:format=MM/dd/yyyy hh\:mm\:ss.fff tt}] ${gdc:item=SoftwareName} Version ${gdc:item=SoftwareVersion}  -  ${message}" />
  <variable name="LogLayout" value="[${date:format=MM/dd/yyyy hh\:mm\:ss.fff tt}] ${message}" />
  <variable name="logDir" value="C:/Logfiles/" />
  <variable name="ArchiveDir" value="C:/Logfiles/Archive" />
  <variable name="Line" value="" />

  <targets async="false">
    <target name="Errors" xsi:type="File" fileName="${logDir}/${var:Line}errors.log" layout="${DebugInfoLayout}" keepFileOpen="false" archiveFileName="${ArchiveDir}/errors_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30" archiveOldFileOnStartup="true" />
    <target name="Info" xsi:type="File" fileName="${logDir}/${var:Line}info.log" layout="${InfoLayout}"  keepFileOpen="false" archiveFileName="${ArchiveDir}/info_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30"/>
    <target name="Debug" xsi:type="File" fileName="${logDir}/${var:Line}debug.log" layout="${DebugInfoLayout}"  keepFileOpen="false" archiveFileName="${ArchiveDir}/debug_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30" />  
  </targets>

  <rules>
    <logger name="Errors" minlevel="Trace" maxlevel="Fatal" writeTo="Errors" />
    <logger name="Info" minlevel="Trace" maxlevel="Warn" writeTo="Info" />
    <logger name="Debug" minlevel="Trace" maxlevel="Fatal" writeTo="Debug" />
  </rules>

</nlog>

我要更新的变量名为“Line”,我有以下代码:

NLog.GlobalDiagnosticsContext.Set("Line", "myLine")

但是 - 日志文件名始终是“debug.log”而不是“myLinedebug.log”。

【问题讨论】:

    标签: asp.net vb.net nlog


    【解决方案1】:

    代替

    GlobalDiagnosticsContext.Set("Line", "myLine")`
    

    使用

    LogManager.Configuration.Variables["line"] = "myLine"
    

    虽然 GlobalDiagnosticsContext 和变量使用相似的概念,但它们并不相同。 GlobalDiagnosticsContext 用于 ${gdc} 渲染器。

    the docs of ${var}

    【讨论】:

    • 太棒了!!!非常感谢。我不敢相信我错过了那个,因为我知道那是 gdc 上下文。再次感谢朱利安。
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多