尝试在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>
我想要更新的变量名为&#34; Line&#34;我有以下代码:
NLog.GlobalDiagnosticsContext.Set("Line", "myLine")
但是 - 日志文件名始终是&#34; debug.log&#34;而不是&#34; myLinedebug.log&#34;。
答案 0 :(得分:3)
而不是
GlobalDiagnosticsContext.Set("Line", "myLine")`
使用
LogManager.Configuration.Variables("line") = "myLine"
虽然GlobalDiagnosticsContext和变量使用相同的概念,但它们并不相同。 GlobalDiagnosticsContext
用于${gdc}
渲染器。