1

I've seen plenty of examples of using the NLog Configuration API to create multiple targets that are linked to multiple rules. However, I cannot find any documentation on how to use the NLog Configuration API to set up a wrapper target around another target. This is the configuration I'm trying to create programmatically with the API:

<targets>
    <target xsi:type="BufferingWrapper" 
        name="InfoBufferingTarget"
        bufferSize="100"
        flushTimeout="60000"
        slidingTimeout="true">

        <target xsi:type="File"
          name="InfoFileTarget" 
          fileName="nlog.log"
          layout="${message}"
          keepFileOpen="true"
          openFileCacheSize="10"
          bufferSize="327680"
          networkWrites="true"
          createDirs="true"
          />
    </target>
</targets>

<rules>
  <logger name="*" minlevel="Info" writeTo="InfoBufferingTarget" />
</rules>

I've got the code working with just the file target, but not exactly sure how to add the buffering wrapper.

Thanks for any help.

M.Bosse
  • 21
  • 3

1 Answers1

1

This is what I use in my ASP.Net Core application:

loggerFactory.AddNLog();
var configuration = LogManager.Configuration;

var target = new FileTarget {
    Name = "log",
    FileName = "logs/logging.log",
    Layout = "${longdate}|${eventproperties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}",
    ArchiveEvery = FileArchivePeriod.Day
}

var wrappedTarget = new AsyncTargetWrapper {
    Name = "wrappedTarget",
    WrappedTarget = target
};

configuration.AddTarget(wrappedTarget);
JD987
  • 341
  • 2
  • 8