.NET
Collect logs from your .NET projects using NLog.
We wrote a tutorial on how to use NLog. We recommend reading it first if you're not familiar with NLog.
  1. 1.
    Install the Logtail NuGet package: dotnet add package Logtail
  2. 2.
    Add an extension, target, and rule to your nlog.config like this:
nlog.config
1
<?xml version="1.0" encoding="utf-8" ?>
2
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
autoReload="true"
5
internalLogLevel="Warn"
6
internalLogFile="internal.txt">
7
<extensions>
8
<add assembly="Logtail" />
9
</extensions>
10
​
11
<targets>
12
<target xsi:type="Logtail" name="logtail" sourceToken="YOUR_SOURCE_TOKEN" />
13
</targets>
14
​
15
<rules>
16
<logger name="*" minlevel="Debug" writeTo="logtail" />
17
</rules>
18
</nlog>
Copied!
And that's all πŸŽ‰ You can now use NLog the way you're used to and your logs will be forwarded to Logtail.
Make sure you replace YOUR_SOURCE_TOKEN with your own source token from Logtail.com.

Additional configuration

The Logtail target will send you logs periodically in batches to optimize network traffic with several retries in case of unexpected HTTP errors. You can adjust this behavior by setting the maxBatchSize, flushPeriodMilliseconds, and retries params to your custom values in your config.
1
<target
2
xsi:type="Logtail"
3
name="logtail"
4
sourceToken="YOUR_SOURCE_TOKEN"
5
maxBatchSize="200"
6
flushPeriodMilliseconds="1000"
7
retries="3" />
Copied!

Structured logging with NLog

All of the properties that you pass to the log will be stored in a structured form in the context section of the logged event. For example, this snippet
1
logger.Info(
2
"The answer to the meaning of life, the universe and everything: {answer}",
3
42
4
);
Copied!
will generate a log event similar to:
1
{
2
"dt": "2021-08-30T07:02:15.452487Z"
3
"level": "info",
4
"message": "The answer to the meaning of life, the universe and everything: 42",
5
"context": {
6
"properties": {
7
"answer": 42
8
},
9
"runtime": { ... }
10
}
11
}
Copied!
You can then filter your logs according to the context.properties.answer_integer field in Live Tail or you can use this column in your custom SQL queries to find relevant logs or to create custom reports.
Last modified 3mo ago