11/6/23, 4:13 PM Better Stack .
NET logging | Better Stack Documentation
Sign in Sign up
Documentation
Better Stack .NET logging
Start logging in 2 minutes
Which .NET logger do you want to use to send log to Better Stack?
NLog
Serilog
Send logs to Better Stack using NLog
1. Install
Install Logtail and NLog.Extension.Logging packages:
Dotnet CLI
dotnet add package Logtail
dotnet add package NLog.Extensions.Logging
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:
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 1/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
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"
layout="${message}"
sourceToken="9dzNW5PYN4TNE7rh3ZLMSAW8" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logtail" />
</rules>
</nlog>
Your source token is included
The code above includes your production source token.
Only you can see it.
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 🎉
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 2/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
Use NLog logger as usual:
Send logs to Logtail
// Configures NLog to color properties based on their type, you can remove it if yo
NLog.Config.ConfigurationItemFactory.Default.ValueFormatter = new Logtail.NLog.Colo
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.
.NET version 5 or higher is required.
Send logs to Better Stack using Serilog
1. Install
Install Logtail , Serilog and Serilog.Sinks.NLog packages:
Dotnet CLI
dotnet add package Logtail
dotnet add package Serilog
dotnet add package Serilog.Sinks.NLog
Using Visual Studio?
Install packages in Tools → Manage NuGet Packages... .
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 3/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
2. Setup
Set up 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="Trace"
internalLogFile="internal.txt">
<extensions>
<add assembly="Logtail" />
</extensions>
<targets>
<target xsi:type="Logtail"
name="logtail"
layout="${message}"
sourceToken="9dzNW5PYN4TNE7rh3ZLMSAW8" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logtail" />
</rules>
</nlog>
Your source token is included
The code above includes your production source token.
Only you can see it.
Add the following configuration to your .csproj file:
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 4/8
🎉
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
3. Start logging
Use Serilog logger with configured .WriteTo.NLog() sink:
Send logs to Logtail
using Serilog;
Log.Logger = new LoggerConfiguration()
.WriteTo.NLog()
.MinimumLevel.Info()
.CreateLogger();
Log.Error("Something bad happened.");
Log.Info("Log message with structured logging. {object}", new {
item="Orange Soda",
price=100.00,
});
Log.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.
.NET version 5 or higher is required.
Need help?
🙏
Please let us know at [email protected].
We're happy to help!
Previous article Next article
Better Stack Java logging Better Stack Heroku logging
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 5/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
Explore documentation
Welcome to Logs 👋
Quick start guide
Logs sources
Programming languages
JavaScript & Node.js
Python
Ruby & Rails
PHP
Java
.NET
Cloud platforms
Web servers & Load balancers
Operating systems
Databases & Message queues
Containers & Clusters
Logging & Monitoring tools
HTTP REST API
Integrations
Slack
Log Forwarding
Vector (Recommended)
Fluent Bit
Logstash
Fluentd
Filebeat
Syslog
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 6/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
API
Get started with Logs API
Sources API
Query API
Using Logs
Dashboards
AWS S3-compatible archive
String + JSON format
Live tail query language
Querying data in Better Stack
GeoIP map visualization
Solutions Resources
Log management Help & Support
Uptime monitoring Uptime docs
Incident management Logs docs
Status page
Compare
Company Pingdom
Work at Better Stack Pagerduty
Engineering StatusPage.io
Security Uptime Robot
StatusCake
Opsgenie
Community
VictorOps
Guides
Questions
Comparisons
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 7/8
11/6/23, 4:13 PM Better Stack .NET logging | Better Stack Documentation
Blog
Write for us
Integrations
Dashboards
From the community
What Is Incident Management? Beginner’s Guide
How to Create a Developer-Friendly On-Call Schedule in 7 steps
Explained: All Meanings of MTTR and Other Incident Metrics
New Relic vs. Splunk: a side-by-side comparison for 2023
Splunk vs Elastic/ELK Stack: The Key Differences to Know
Terms of Use Privacy Policy GDPR System status
© 2023 Better Stack, Inc.
https://betterstack.com/docs/logs/net-c/?source=395254#send-logs-to-better-stack-using-serilog 8/8