• Print
  • Share
  • Dark

How to Configure Serilog

  • Updated on 12 Jul 2018
  • 3 minutes to read
  • Contributors

You can easily send your application logs to Retrace if you are using Serilog.

  1. Add our NuGet package
  2. Add our logging sink to your Serilog configuration
  3. Specify your Stackify license key in your configuration

The code for Stackify's .NET library and logging appenders are all available on Github.

Getting Started Using NuGet

Install our nuget package to add our Serilog target to your project.

PM> Install-Package  Serilog.Sinks.Stackify

Add the Stackify ApiKey to your config

In order for the NuGet installation to complete, copy your Stackify Activation Key (license key) from your Stackify account page and paste it here.

Please note that this example is for a .NET Framework config file. If you are using .NET Core, the configuration would be done in the same manner in json.

    <add key="Stackify.ApiKey" value="Your Activation Key" />
    <add key="Stackify.AppName" value="Your App Name"/> <!-- optional - will be inferred by Stackify if this is running on a monitored server -->
    <add key="Stackify.Environment" value="Your Environment"/> <!-- optional - will be inferred by Stackify if this is running on a monitored server -->

The AppName and Environment are not required but are recommended if you are logging from a server without Retrace installed.

Once this is complete, you are done with the installation and configuration of the Stackify Serilog appender.

Basic Appender Configuration

Below is the simplest Serilog configuration needed to log to Retrace.

var stackifyLogger = new LoggerConfiguration()

Logging Custom Properties

In order to add custom properties to your logs, use one of the overloaded logger methods, as shown below. Choose the level of log, the message, and add as many additional properties as needed.

Log.Debug("User Login", new { ID = 65465849, User = "Stackify" });

This is what the above example should look like in Retrace after being run.


Before you can do this, you must add the below class to your program.

class ThreadIdEnricher : ILogEventEnricher
    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
                "ThreadId", Thread.CurrentThread.ManagedThreadId));

Then, add a method call .Enrich.WithProperty("property", "property info") to the creation of the logger object, like so.

var stackifyLogger = new LoggerConfiguration()
stackifyLogger.Fatal("Serilog to Stackify, Fatal level");

The above example would look like this in Retrace: image.png

You can add as many properties as you would like by simply calling the method several times when creating the logger. For more info on enriching, visit Serilog's github page.

Control Which Logs are Sent to Stackify

If you send your logs to multiple places and want to change what goes where, you can do that, even without creating a seperate logger object. All you need to do is add multiple write methods to the logger when it's created, and restrict their minimum logging level, as shown below.

var stackifyLogger = new LoggerConfiguration()
                .WriteTo.Stackify(restrictedToMinimumLevel: LogEventLevel.Debug)
                .WriteTo.File("errorlogs.txt", restrictedToMinimumLevel: LogEventLevel.Error)

In this example, the logs from the same logger object are being writen to both Stackify and to a local file. Stackify will receive everything while the text file will only contain logs error level and above.

Recording Context Values

If you are using logical or thread context keys within Serilog and would like to collect and send those to Retrace, you can. Review our code to get a better idea of how it works.

var stackifyLogger = new LoggerConfiguration()
                .WriteTo.Stackify(restrictedToMinimumLevel: LogEventLevel.Debug)
stackifyLogger.ForContext("UserID", id.ToString()).Error("login");

The above example would look like this in Retrace: image.png

View Your Logs With Prefix

You can use Prefix to view your logs on your workstation. It requires adding the Serilog appender to pick up and show your logs within Prefix. You don't even need to configure a Stackify ApiKey for Prefix to work properly!


If you are having issues getting Errors and Logs to come into your account, please see Troubleshoot: Errors and Logs .NET Configurations

Was this article helpful?