Better Stack .NET logging

Start logging in 2 minutes

Which .NET logger do you want to use to send log to Better Stack?

Send logs to Better Stack using NLog

1. Install

Install NLog.Extension.Logging and BetterStack.Logs.NLog packages:

Dotnet CLI
dotnet add package NLog.Extensions.Logging
dotnet add package BetterStack.Logs.NLog

Using Visual Studio?
Install packages in Tools β†’ Manage NuGet Packages....

2. Setup

Set up NLog logging with Better Stack. 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="BetterStack.Logs.NLog" />
    </extensions>

    <targets>
        <target xsi:type="BetterStack.Logs"
            name="mybetterstack"
            layout="${message}"
            sourceToken="$SOURCE_TOKEN" />
    </targets>

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

Add the following configuration to your .csproj file:

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

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

3. Start logging πŸŽ‰

Use NLog logger as usual:

Send logs to Better Stack
// Configures NLog to color properties based on their type, you can remove it if you don't want it
NLog.LogManager.Setup().SetupSerialization(
  setupBuilder => setupBuilder.RegisterValueFormatter(new BetterStack.Logs.NLog.ColorValueFormatter())
);

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 Better Stack Logs β†’ Live tail.
Find passed structured data under the context.properties field.

.NET version 5 or higher is required.

Send logs to Better Stack using Serilog

1. Install

Install Serilog.Extensions.Logging and BetterStack.Logs.Serilog packages:

Dotnet CLI
dotnet add package Serilog.Extensions.Logging
dotnet add package BetterStack.Logs.Serilog

Using Visual Studio?
Install packages in Tools β†’ Manage NuGet Packages....

2. Configure Serilog and start logging πŸŽ‰

Use Serilog logger with configured .WriteTo.BetterStack() sink:

Send logs to Better Stack
using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.BetterStack(sourceToken: "$SOURCE_TOKEN")
    .MinimumLevel.Info()
    .CreateLogger();

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

Log.CloseAndFlush();

You should see your logs in Better Stack Logs β†’ Live tail.
Find passed structured data under the properties field.

.NET version 5 or higher is required.

Additional information

You can see example projects for both NLog and Serilog integration.

Using Serilog

For logs to be send to Better Stack reliably, Serilog must be properly closed by Log.CloseAndFlush(), even when application ends unexpectadly. For more information, see Lifecycle of Loggers in Serilog docs.

For more on Serilog configuration, see their official docs.

Need help?

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