# Better Stack .NET logging

## Start logging in 2 minutes

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

- [NLog](#send-logs-to-better-stack-using-nlog)
- [Serilog](#send-logs-to-better-stack-using-serilog)

## Send logs to Better Stack using NLog

### 1. Install

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

```bash
[label Dotnet CLI]
dotnet add package NLog.Extensions.Logging
dotnet add package BetterStack.Logs.NLog
```

[info]
**Using Visual Studio?**  
Install packages in `Tools` → `Manage NuGet Packages...`.
[/info]

### 2. Setup

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

```xml
[label 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"
            endpoint="https://$INGESTING_HOST" />
    </targets>

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

Add the following configuration to your `.csproj` file:

```xml
[label 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`.
[/info]

### 3. Start logging 🎉

Use NLog logger as usual:

```cs
[label 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](https://telemetry.betterstack.com/team/0/tail ";_blank").  
Find passed structured data under the `context.properties` field.

[warning]
**.NET version 5 or higher is required.**  
[/warning]

## Send logs to Better Stack using Serilog

### 1. Install

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

```bash
[label Dotnet CLI]
dotnet add package Serilog.Extensions.Logging
dotnet add package BetterStack.Logs.Serilog
```

[info]
**Using Visual Studio?**  
Install packages in `Tools` → `Manage NuGet Packages...`.
[/info]

### 2. Configure Serilog and start logging 🎉

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

```cs
[label Send logs to Better Stack]
using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.BetterStack(
        sourceToken: "$SOURCE_TOKEN",
        betterStackEndpoint: "https://$INGESTING_HOST"
    )
    .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](https://telemetry.betterstack.com/team/0/tail ";_blank").  
Find passed structured data under the `properties` field.

[warning]
**.NET version 5 or higher is required.**  
[/warning]

## Additional information

You can see example projects for both [NLog](https://github.com/BetterStackHQ/logs-client-nlog/tree/main/example-project) and [Serilog](https://github.com/BetterStackHQ/logs-client-serilog/tree/main/example-project) 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](https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers) in Serilog docs.

For more on Serilog configuration, see [their official docs](https://github.com/serilog/serilog/wiki/Configuration-Basics).

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏