Krzysztof Skowronek
Krzysztof Skowronek

Reputation: 2936

Use nlog to push logs directly to Grafana Loki Cloud

I have an old WPF app that I want to add some alerting to, so I created a free account in Grafana Cloud and I want to be able to see logs there.

The app is using nlog, so I added a sink in nlog.config and NLog.Loki nuget package:

 <extensions>
        <add assembly="NLog.Loki" />
 </extensions>

 <target xsi:type="Loki"
                name="loki"
                endpoint="https://<my_client_id>:<my_access_token>@logs-prod-012.grafana.net/loki/api/v1/push"
                >

but my logs are not showing up in the cloud. In nlog internal log I can the see the following:

2023-09-28 23:37:00.9032 Warn lokiTarget(Name=loki): WriteAsyncTask failed on completion. Sleep 250 ms Exception: System.Net.Http.HttpRequestException: Failed pushing logs to Loki.
   in NLog.Loki.HttpLokiTransport.<ValidateHttpResponse>d__7.MoveNext()
--- End of stacktrace ---
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter.GetResult()
   in NLog.Loki.HttpLokiTransport.<WriteLogEventsAsync>d__4.MoveNext()

What am I doing wrong? I tried different url combinations I found online, ChatGPT went on a loop that it should work and I'm a bit lost.

I am new to Grafana Cloud, so maybe I need to change some configuration there? All seems fine - the Loki data source is there, and proudly shows "No data":

enter image description here

Upvotes: 1

Views: 1696

Answers (1)

Krzysztof Skowronek
Krzysztof Skowronek

Reputation: 2936

I figured it out, and it is super easy.

New config:

<target name="loki-cloud" xsi:type="loki" endpoint="https://logs-prod-012.grafana.net" username="{you-user}"
                password="{your-password}"
                layout="${structuredlogging.json}">
            <label name="app" layout="something" />
        </target>

so, as you can see, the endpoint just needs the hostname, it will append the API route.

Also, you always need at least one label for Loki to accept the logs

Upvotes: 2

Related Questions