Build status NuGet Samples

Logging to from Serilog

Serilog is a great addition to the flowering .NET logging community, described as “A no-nonsense logging library for the NoSQL era” on their project page. Serilog works just like other logging frameworks such as log4net and NLog, but offers a great fluent API and the concept of sinks (a bit like appenders in log4net). Sinks are superior to appenders, because they threat errors as objects rather than strings, a perfect fit for which itself is built on NoSQL. Serilog already comes with native support for, which makes it easy to integrate with any application using Serilog.

In this example we’ll use a ASP.NET MVC project as an example. Neither Serilog nor are bound to log errors from web applications. Adding this type of logging to your windows and console applications is just as easy. Add the Serilog.Sinks.ElmahIo NuGet package to your project:

Install-Package Serilog.Sinks.ElmahIo

To configure Serilog, add the following code to the Application_Start method in global.asax.cs:

var log =
    new LoggerConfiguration()
        .WriteTo.ElmahIo("API_KEY", new Guid("LOG_ID"))
Log.Logger = log;

Replace API_KEY with your API key (Where is my API key?) and LOG_ID with the ID of the log you want messages sent to (Where is my log ID?).

First, we create a new LoggerConfiguration and tell it to write to The log object can be used to log errors and you should register this in your IoC container. In this case, we don’t use IoC, that is why the log object is set as the public static Logger property, which makes it accessible from everywhere.

To log log exceptions to through Serilog, is the Log class provided by Serilog:

try {
    // Do some stuff which may cause an exception
catch (Exception e) {
    Log.Error(e, "The actual error message");

The Error method tells Serilog to log the error in the configured sinks, which in our case logs to Simple and beautiful.

Logging custom properties

Serilog support logging custom properties in three ways: As part of the log message, through enrichers and using LogContext. All three types of properties are implemented in the sink as part of the Data dictionary to

The following example shows how to log all three types of properties:

var logger =
    new LoggerConfiguration()
        .Enrich.WithProperty("ApplicationIdentifier", "MyCoolApp")
        .WriteTo.ElmahIO(new Guid("a6ac10b1-98b3-495f-960e-424fb18e3caf"))

using (LogContext.PushProperty("ThreadId", Thread.CurrentThread.ManagedThreadId))
    logger.Error("This is a message with {Type} properties", "custom");

Beneath the Data tab on the logged message details, the ApplicationIdentifier, ThreadId and Type properties can be found.

Serilog.Sinks.ElmahIo provides a range of reserved property names, that can be used to fill in data in the correct fields on the UI. Let's say you want to fill the User field using structured logging only:

logger.Information("{Quote} from {User}", "Hasta la vista, baby", "Arnold Schwarzenegger");

This will fill in the value Arnold Schwarzenegger in the User field, as well as add two key/value pairs (Quote and User) to the Data tab on For a reference of all possible property names, check out the property names on CreateMessage.


Serilog provides a package for ASP.NET Core, that routes log messages from inside core through Serilog. We recommend to use this package together with the sink, in order to capture warnings and errors happening inside ASP.NET Core.

To use this, install the following packages:

Install-Package Serilog.AspNetCore -DependencyVersion Highest
Install-Package Elmah.Io.Client
Install-Package Serilog.Sinks.ElmahIo

(We install the Elmah.Io.Client manually, to make sure we run on the most recent version)

Configure Serilog as usual:

Log.Logger = new LoggerConfiguration()
    .WriteTo.ElmahIo("API_KEY", new Guid("LOG_ID"), LogEventLevel.Warning)

Finally, call the UseSerilog-method:

public static IWebHost BuildWebHost(string[] args) =>

Now, all warnings, errors and fatals happening inside ASP.NET Core are logged to

Config using appsettings.json

While Serilog provides a great fluent C# API, some prefer to configure Serilog using an appsettings.json file. To configure the sink this way, you will need to install the Serilog.Settings.Configuration NuGet package. Then configure in your appsettings.json file:

        "MinimumLevel": "Warning",
                "Name": "ElmahIo",
                    "apiKey": "API_KEY",
                    "logId": "LOG_ID"

Make sure to specify the apiKey and logId arguments with the first character in lowercase.

Finally, tell Serilog to read the configuration from the appsettings.json file:

var configuration = new ConfigurationBuilder()

var logger = new LoggerConfiguration()

This article was brought to you by the team. is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website