.NET Core NLog to Graylog via GELF not working

318 views Asked by At

I am trying to change my NLog targets from files to Graylog to have centralized logging. But it doesnt work. I am currently logging my web projects, SQL Server, Domain controller and more to my graylog, but those web services are using Serilog which was pretty easy to setup. But on my microservices I use NLog which doesnt work as well for me.

My NLog config looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="solar" xsi:type="Gelf" host="10.0.0.43" port="12201" protocol="Tcp">
            <layout xsi:type="GelfLayout">
                <parameter name="tags" layout="MySolarService" />
            </layout>
        </target>
        </targets>

    <rules>
        <logger name="MySolarService" minlevel="Trace" writeTo="solar" />
    </rules>
</nlog>

And my code:

public class Service
    {
        private static readonly ILogger _logger = NLogBuilder.ConfigureNLog(@"C:\MySolarService\MySolarService\NLog.config").GetLogger("MySolarService");
        readonly SolarService _solarService = new ();
        readonly System.Timers.Timer timer_60s = new ()
        {
            Enabled = true,
            Interval = 1000,
        };
        public void Run()
        {
            timer_60s.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (source == timer_60s)
            {
                timer_60s.Interval = 60000;
                TodoTimer60sAsync();
            }
        }
        
        public async Task TodoTimer60sAsync()
        {
            _logger.Error("TJENA GRAYLOGGG MY MAN");
        }
}

But I dont recive anything on my graylog side, is there a way to see if the log succeeds to a specific taget? I tried adding a new target to a file for every log and that works flawless. But still not reciving anything on graylog.

My serilog in my web services are setup like this:

Serilog.Log.Logger = new LoggerConfiguration()
            .Enrich.With(new TagsEnricher("MyWebService"))
            .MinimumLevel.Warning()
            .WriteTo.GraylogGelf("10.0.0.43", 12201, TransportType.Tcp)
            .CreateLogger();
1

There are 1 answers

0
Rolf Kristensen On

Think you have added too much GELF into the attempt. Either use xsi:type="Gelf" or use xsi:type="GelfLayout" (But not both at the same time).

Example with NLog.GelfLayout and NLog NetworkTarget (Supports TCP + UDP + HTTP):

<target name="solar"
        xsi:type="Network"
        address="tcp://10.0.0.43:12201"
        newLine="true"
        lineEnding="Null">
    <layout xsi:type="GelfLayout">
        <field name="tags" layout="MySolarService" />
    </layout>
</target>

Example with NLog.Web.AspNetCore.Targets.Gelf (Only supports UDP and not TCP/HTTP)

<target name="solar" 
        xsi:type="Gelf"
        endpoint="udp://10.0.0.43:12201"
        facility="solar"
        gelfVersion="1.1">
    <parameter name="tags" layout="MySolarService" />
</target>

Example with NLog.Targets.GraylogHttp (Only supports HTTP and not TCP/UDP):

<target name="solar"
        xsi:type="GraylogHttp"
        facility="solar"
        graylogServer="http://10.0.0.43"
        graylogPort="12201">
        <parameter name="tags" layout="MySolarService" />
</target>

When troubleshooting issues with NLog, then it is recommended to add throwConfigExceptions="true" and activate NLog InternalLogger and look for clues. See also: https://github.com/NLog/NLog/wiki/Logging-Troubleshooting