I'm using the HttpEventCollectorTraceListener and originally my code looked like this:
using System;
using System.Configuration;
using System.Diagnostics;
using NLog;
using Splunk.Logging;
namespace SplunkDiscovery
{
static class Program
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
Trace.Listeners.Clear();
Trace.Listeners.Add(CreateSplunkListener());
Logger.Info("Trace Listener setup. Test app started with {0} argument(s)", args.Length);
}
private static HttpEventCollectorTraceListener CreateSplunkListener()
{
var listener = new HttpEventCollectorTraceListener(
new Uri(ConfigurationManager.AppSettings["SplunkUrl"], UriKind.Absolute),
ConfigurationManager.AppSettings["AppToken"]);
return listener;
}
}
}
And an app.config like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<appSettings>
<add key="AppToken" value="28BD5B15-CF19-416E-B147-BCB4DDBC83DB"/>
<add key="SplunkUrl" value="https://splunk:8088/"/>
</appSettings>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target name="splunkTraceTarget" xsi:type="Trace" layout="${time}|${level:uppercase=true}|${logger}::${message}" />
<target name="debugger" xsi:type="Debugger" layout="${time}|${level:uppercase=true}|${logger}::${message}" />
</targets>s
<rules>
<logger name="*" minlevel="Debug" writeTo="splunkTraceTarget" />
</rules>
</nlog>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
To me this should work since NLog target is set to Trace and I added the HTTP event collector trace listener and logged something, but it doesn't work.
The only way I can make it work is a hacky combination of adding Trace.WriteLine("hi"); and follow that with Trace.Close(); and then on the listener set batchInterval to 1 and batchSizeCount to 1.
So basically, nothing gets logged unless the batch size is met and something is written directly to Trace, without going through NLog to write to Trace.
Also, obviously those batch sizes aren't realistic for performance reasons. I've tried Flush(), Close(), and Dispose() at both the trace listener level and the Trace level, but that doesn't result in the data getting sent.
... View more