简体   繁体   English

NLog 布局渲染器似乎在配置中不起作用

[英]NLog layout renderers don't seem to work in configuration

Using ASP.NET v4.7.2, in nlog.config I have this target:使用 ASP.NET v4.7.2,在nlog.config我有这个目标:

<target name="logfile" xsi:type="File" fileName="{gdc:item=logFileName}" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"/>

In Global.asax.cs:Application_Start I have this:Global.asax.cs:Application_Start我有这个:

GlobalDiagnosticsContext.Set("logFileName", @"c:\\Temp\\nlog-all-${shortdate}.txt");

And just for good measure:只是为了更好的衡量:

LogManager.Configuration = LogManager.Configuration.Reload();

Putting a breakpoint on the line immediately following, and digging into the LogManager.Configuration properties, I find that the File log target has this for its FileName property:在紧随其后的行上放置一个断点,并深入研究LogManager.Configuration属性,我发现File日志目标的FileName属性具有此属性:

FileName="{gdc:item=logFileName}"

When I write to the logger, it creates a file in the application root called--you guessed it-- {gdc:item=logFileName} .当我写入记录器时,它会在应用程序根目录中创建一个名为 - 你猜对了 - {gdc:item=logFileName}

In the Command window, at a breakpoint immediately following the GDC set, I executed the null log action recommended by Mr Kristensen in his answer:在命令窗口中,在 GDC 设置之后的断点处,我执行了 Kristensen 先生在他的回答中推荐的空日志操作:

((NLog.Targets.FileTarget)LogManager.Configuration.AllTargets[1]).FileName.Render(NLog.LogEventInfo.CreateNullEvent())

The result was {gdc:item=logFileName}结果是{gdc:item=logFileName}

In other words, it looks like the GDC layout renderer isn't working.换句话说,看起来 GDC 布局渲染器不起作用。

What am I doing wrong here?我在这里做错了什么?

Works as intended.按预期工作。 FileName-property is a NLog Layout. FileName-property 是一个 NLog 布局。 Means it can resolve its value dynamically depending on LogEvent.意味着它可以根据 LogEvent 动态解析其值。 When looking at the FileName-property using the debugger then you are not providing a LogEvent.使用调试器查看 FileName 属性时,您没有提供 LogEvent。

Try doing this in a debug-watch (Remember to correct {gdc} to ${gdc} )尝试在调试监视中执行此操作(记住将{gdc}更正为${gdc}

fileTarget.FileName.Render(NLog.LogEventInfo.CreateNullEvent());

NLog Layout engine allows you to capture all kind of context-information, without adding it to the actual LogEvent. NLog 布局引擎允许您捕获所有类型的上下文信息,而无需将其添加到实际的 LogEvent。 But can also extract context from the LogEvent .但也可以从 LogEvent 中提取上下文

See also https://nlog-project.org/config/?tab=layout-renderers and https://nlog-project.org/config/?tab=layouts另见https://nlog-project.org/config/?tab=layout-renderershttps://nlog-project.org/config/?tab=layouts

But ${gdc} is intended for global-variables and not for embedding layout-renderers.但是${gdc}用于全局变量而不是用于嵌入布局渲染器。 Instead you could configure like this:相反,您可以这样配置:

<target name="logfile" xsi:type="File" fileName="${gdc:item=logDirectory}/nlog-all-${shortdate}.txt" ... />

And assign the GDC variable like this:并像这样分配 GDC 变量:

GlobalDiagnosticsContext.Set("logDirectory", @"c:\\Temp");

If you absolutely need layout-renderer logic embedded in the value, then you should consider using ${var} instead of ${gdc} .如果您绝对需要在值中嵌入布局渲染器逻辑,那么您应该考虑使用${var}而不是${gdc}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM