Nlog: Performance counter layout renderer parse bug

Created on 30 Aug 2019  ·  9Comments  ·  Source: NLog/NLog

Nlog version: 4.6.7

Platform: .Net 4.7

Current NLog config (xml or C#, if relevant)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="c:/logs/nlog.log" internalLogLevel="Trace">
    <variable name="facilityName" value="BppprCalc" />
    <targets async="true">
        <target name="graylogCounters" xsi:type="GraylogHttp" facility="${facilityName}" graylogServer="http://graylog" graylogPort="12201">
        <parameter name="processor_total" layout="${performancecounter:Category=Processor:Counter=% Processor Time:Instance=_Total}" />
        <parameter name="requests_per_second" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Requests/Sec:Instance=${gdc:InstanceName}}" />
        <parameter name="requests_in_queue" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Requests in Application Queue:Instance=${gdc:InstanceName}}" />
        <parameter name="errors_total_per_second" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Errors Total/Sec:Instance=${gdc:InstanceName}}" />
      </target>
    </targets>
    <rules>
      <logger name="counters" level="debug" writeTo="graylogCounters" final="true"/>
    </rules>
  </nlog>
  • What is the current result?
    Layout performance counter gives an error while parsing(I think) gdc:. I have putted variables in gdc and value is all right.
    This is internal logs of setup.
2019-08-30 13:19:16.5796 Warn Exception in layout renderer. Exception: System.InvalidOperationException: Экземпляр '${gdc' не существует в указанной категории.
  в System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
  в System.Diagnostics.PerformanceCounter.NextSample()
  в NLog.LayoutRenderers.PerformanceCounterLayoutRenderer.GetValue()
  в NLog.LayoutRenderers.PerformanceCounterLayoutRenderer.Append(StringBuilder builder, LogEventInfo logEvent)
  в NLog.LayoutRenderers.LayoutRenderer.RenderAppendBuilder(LogEventInfo logEvent, StringBuilder builder)

In english the result is the gdc do not exist in selected category.

  • What is the expected result?
    performance counters layout renderer works well and can get data from gdc.
  • I also tried to add a variable into target and reconfigure my config at application startup(global.asax).
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="c:/logs/nlog.log" internalLogLevel="Trace">
    <variable name="facilityName" value="BppprCalc" />
    <variable name="instanceName" value="DefaultInstance" />
    <targets async="true">
      <target name="graylogCounters" xsi:type="GraylogHttp" facility="${facilityName}" graylogServer="http://graylog" graylogPort="12201">
        <parameter name="processor_total" layout="${performancecounter:Category=Processor:Counter=% Processor Time:Instance=_Total}" />
        <parameter name="requests_per_second" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Requests/Sec:Instance=${instanceName}}" />
        <parameter name="requests_in_queue" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Requests in Application Queue:Instance=${instanceName}}" />
        <parameter name="errors_total_per_second" layout="${performancecounter:Category=ASP.NET Apps v4.0.30319:Counter=Errors Total/Sec:Instance=${instanceName}}" />
      </target>
    </targets>
    <rules>
      <logger name="counters" level="debug" writeTo="graylogCounters" final="true"/>
    </rules>
  </nlog>

code at startup
```c#
NLog.LogManager.Configuration.Variables["instanceName"] = "_LM_W3SVC_1_ROOT_BppprCalc";
NLog.LogManager.ReconfigExistingLoggers();

and there is another mistake

2019-08-30 14:04:12.7192 Warn Exception in layout renderer. Exception: System.InvalidOperationException: Экземпляр 'DefaultInstance' не существует в указанной категории.
в System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
в System.Diagnostics.PerformanceCounter.NextSample()
в NLog.LayoutRenderers.PerformanceCounterLayoutRenderer.GetValue()
в NLog.LayoutRenderers.PerformanceCounterLayoutRenderer.Append(StringBuilder builder, LogEventInfo logEvent)
в NLog.LayoutRenderers.LayoutRenderer.RenderAppendBuilder(LogEventInfo logEvent, StringBuilder builder)
```

question

All 9 comments

You have correctly spotted that Instance is not implemented with support for NLog Layout:

https://github.com/NLog/NLog/blob/9358d3db57027676a2d891a10b1049e8131e89c0/src/NLog/LayoutRenderers/PerformanceCounterLayoutRenderer.cs#L74

PullRequest is welcome.

Guess this is #1234 ?

Well implementing #1234 so it works great everywhere is not an easy task. Implementing good dynamic caching logic is not easy, when it has to perform and be predictable.

Also Layout loves to render string-objects, so problem solved here (Instance is a string). Think #1234 is about making it easy to also render other datatypes (bool + integer + enum + etc.).

Well, actually I tried to make a variable, set value and reconfigure the target in order to get right performance counter.
c# NLog.LogManager.Configuration.Variables["instanceName"] = "_LM_W3SVC_1_ROOT_BppprCalc"; NLog.LogManager.ReconfigExistingLoggers();
But strange things happens. layout is wrong
image

but variable is ok
image

Well, actually I tried to make a variable, set value and reconfigure the target in order to get right performance counter.

You are very persistent in making this work, but the NLog-magic only works with Layout. And Instance is not implemented with support for NLog Layout:

https://github.com/NLog/NLog/blob/9358d3db57027676a2d891a10b1049e8131e89c0/src/NLog/LayoutRenderers/PerformanceCounterLayoutRenderer.cs#L74

PullRequest is welcome.

Created #3579 with example of how to implement NLog Layout magic.

Has your question being answered?

I assume your question has been answered, if not, please let us know!

@Kirya522 NLog ver. 4.6.8 has been released that allows NLog Layout-logic to work for the Instance-property:

https://www.nuget.org/packages/NLog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haythamabutair picture haythamabutair  ·  3Comments

Rapiiidooo picture Rapiiidooo  ·  3Comments

geedsen picture geedsen  ·  3Comments

ErcinDedeoglu picture ErcinDedeoglu  ·  3Comments

JustArchi picture JustArchi  ·  3Comments