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