Build status NuGet Samples

Logging to elmah.io 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 elmah.io which itself is built on NoSQL. Serilog already comes with native support for elmah.io, 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 elmah.io 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(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID")))
        .CreateLogger();
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 elmah.io. 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 elmah.io 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 elmah.io. 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 elmah.io sink as part of the Data dictionary to elmah.io.

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

var logger =
    new LoggerConfiguration()
        .Enrich.WithProperty("ApplicationIdentifier", "MyCoolApp")
        .Enrich.FromLogContext()
        .WriteTo.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID")))
        .CreateLogger();

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 elmah.io 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 elmah.io. For a reference of all possible property names, check out the property names on CreateMessage.

Message hooks

Serilog.Sinks.ElmahIo provide message hooks similar to the integrations with ASP.NET and ASP.NET Core.

Message hooks require Serilog.Sinks.ElmahIo version 3.3.0 or newer.

Decorating log messages

To include additional information on log messages, you can use the OnMessage event when initializing the elmah.io target:

Log.Logger =
    new LoggerConfiguration()
        .WriteTo.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID"))
        {
            OnMessage = msg =>
            {
                msg.Version = "1.0.0";
            }
        })
        .CreateLogger();

The example above includes a version number on all errors. Since the elmah.io sink also picks up encrichers specified with Serilog, this example could be implemented by enriching all log messages with a field named version.

Handle errors

To handle any errors happening while processing a log message, you can use the OnError event when initializing the elmah.io sink:

Log.Logger =
    new LoggerConfiguration()
        .WriteTo.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID"))
        {
            OnError = (msg, ex) =>
            {
                Console.Error.WriteLine(ex.Message);
            }
        })
        .CreateLogger();

The example implements a callback if logging to elmah.io fails. How you choose to implement this is entirely up to your application and tech stack.

Error filtering

To ignore specific messages based on their content, you can use the OnFilter event when initializing the elmah.io sink:

Log.Logger =
    new LoggerConfiguration()
        .WriteTo.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID"))
        {
            OnFilter = msg =>
            {
                return msg.Title.Contains("trace");
            }
        })
        .CreateLogger();

The example above ignores any log message with the word trace in the title.

ASP.NET Core

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 elmah.io 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 Serilog.Sinks.ElmahIo

Configure Serilog as usual:

public static int Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .WriteTo.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID"))
        {
            MinimumLogEventLevel = Events.LogEventLevel.Warning
        })
        .CreateLogger();

    try
    {
        CreateWebHostBuilder(args).Build().Run();
        return 0;
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "Host terminated unexpectedly");
        return 1;
    }
    finally
    {
        Log.CloseAndFlush();
    }
}

Finally, call the UseSerilog-method in BuildWebHost:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost
        .CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseSerilog();

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

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 elmah.io sink this way, you will need to install the Serilog.Settings.Configuration NuGet package. Then configure elmah.io in your appsettings.json file:

{
    ...
    "Serilog":{
        "Using":[
            "Serilog.Sinks.ElmahIo"
        ],
        "MinimumLevel": "Warning",
        "WriteTo":[
            {
                "Name": "ElmahIo",
                "Args":{
                    "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()
    .AddJsonFile("appsettings.json")
    .Build();

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

This article was brought to you by the elmah.io team. elmah.io 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