Better Stack .NET logging

Start logging in 2 minutes

Collect logs from C# applications.

1. Install

Install Logtail and NLog.Extension.Logging packages:

Dotnet CLI
dotnet add package Logtail
dotnet add package NLog.Extensions.Logging
info

Using Visual Studio?
Install packages in Tools -> Manage NuGet Packages....

2. Setup

Set up NLog logging with Logtail. Save the following config as nlog.config in your project root:

NLog config
<?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"
        autoReload="true"
        internalLogLevel="Warn"
        internalLogFile="internal.txt">
    <extensions>
        <add assembly="Logtail" />
    </extensions>

    <targets>
        <target xsi:type="Logtail" name="logtail" sourceToken="$SOURCE_TOKEN" />
    </targets>

    <rules>
        <logger name="*" minlevel="Trace" writeTo="logtail" />
    </rules>
</nlog>

3. Setup

Add the following configuration to your .csproj file:

Project config
<ItemGroup>
  <None Update="nlog.config" CopyToOutputDirectory="Always" />
</ItemGroup>
info

Using Visual Studio?
Right-click file nlog.config -> Quick Properties -> Copy to Output Directory.

4. Start logging ๐ŸŽ‰

Use NLog logger as usual:

Send logs to Logtail
var logger = NLog.LogManager.GetCurrentClassLogger();

logger.Error("Something bad happened.");
logger.Info("Log message with structured logging. {object}", new {
  item="Orange Soda",
  price=100.00,
});
logger.Info("User has bought {item} for {price}.", "Orange Soda", 100.00);

You should see your logs in Logtail -> Live tail.
Find passed structured data under the context.properties field.

warning

.NET version 5 or higher is required.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! ๐Ÿ™