Search code examples
c#nlog

NLog default wrapper usage in JSON-config


Documentation says:

Sometimes we require ALL targets to be wrapped in the same way, for example to add buffering and/or retrying. NLog provides <default-wrapper /> syntax for that. You simply put this element in the <targets /> section and all your targets will be automatically wrapped with the specified wrapper.

It also provides the following example:

<nlog>  
  <targets>  
    <default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>  
    <target name="f1" xsi:type="File" fileName="f1.txt"/>  
    <target name="f2" xsi:type="File" fileName="f2.txt"/>  
  </targets>  
  <targets>  
    <default-wrapper xsi:type="AsyncWrapper">  
      <wrapper-target xsi:type="RetryingWrapper"/>  
    </default-wrapper>  
    <target name="n1" xsi:type="Network" address="tcp://localhost:4001"/>  
    <target name="n2" xsi:type="Network" address="tcp://localhost:4002"/>  
    <target name="n3" xsi:type="Network" address="tcp://localhost:4003"/>  
  </targets>  
</nlog>

So, <default-wrapper /> element should be a child of <targets> to be applied to each <target> within the same parent.

On the other hand, Extended Json NLog Config Example uses default-wrapper on the same level as targets element:

"default-wrapper": {
    "type": "AsyncWrapper",
    "overflowAction": "Block"
},
"targets": {

So, I have the following questions:

  1. Is the Extended Json NLog Config Example correct?
  2. Will all targets from targets element be wrapped with the default-wrapper, placed out of the targets?
  3. What if there are default-wrapper within targets and default-wrapper out of targets in the same configuration file?

Solution

  • Yes there was some discussion about whether XML-config and JSON-config should be completely 1-to-1. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/283

    Because JSON-config represented targets-section as a dictionary of known target-names, then it didn't feel natural to have default-wrapper and default-target-parameters as reserved magic-strings.

    1. Instead they were moved out of targets-section, as you have discovered from the documentation.
    2. Yes all items in the targets-section will be wrapped, when using default-wrapper.
    3. Have not tested what happens if placing default-wrapper and default-target-parameters inside the targets-section-dictionary. Maybe check the NLog InternalLogger whether it complains about unknown configuration-items.

    Notice more work is being done to give default-wrapper and default-target-parameters a better name, since it might not be easy to guess that they both apply to the targets-section. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/500