asp.net - NLog - 变量更新(在运行时)

标签 asp.net vb.net nlog

尝试在 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>

我试图更新的变量称为“行”,我有以下代码:

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

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

最佳答案

代替

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



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

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

the docs of ${var}

关于asp.net - NLog - 变量更新(在运行时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005080/

相关文章:

asp.net - 如何在另一个下拉列表事件期间刷新下拉列表而不刷新整个网页?

.net - 有没有更简单的方法从两点创建正矩形?

wpf - WPF XAML 中的 My.Resources?

vb.net - vb.net 中的快速 OCR

c# - 如果异常不为空,则 Nlog 输出字符

.net - 如何在运行时更改 NLog 规则目标?

c# - 来自不同项目的 NLog

.net - 加快 ASP.NET 中的构建时间

c# - ASP.net 在一个 ashx 文件中处理自定义文件扩展名的所有请求

asp.net - 身份服务器 4 使用有效访问 token 获取 401 未授权