Build status NuGet Samples

Logging from Microsoft.Extensions.Logging

Microsoft.Extensions.Logging is a common logging abstraction from Microsoft, much like log4net and Serilog. Microsoft.Extensions.Logging started as a new logging mechanism for ASP.NET Core, but now acts as a logging framework for all sorts of project types.

Start by installing the Elmah.Io.Extensions.Logging package:

Install-Package Elmah.Io.Extensions.Logging

Locate your API key (Where is my API key?) and log ID. The two values will be referenced as API_KEY and LOG_ID (Where is my log ID?) in the following.

Logging from ASP.NET Core

Call AddElmahIo in the Configure-method in Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory fac)
    fac.AddElmahIo("API_KEY", new Guid("LOG_ID"));

Start logging messages by injecting an ILogger in your controllers:

public class HomeController : Controller
    private readonly ILogger _logger;

    public HomeController(ILogger logger)
        _logger = logger;

    public IActionResult Index()
        _logger.LogWarning("Request to index");
        return View();

Logging from a console application

Create a new LoggerFactory:

var factory = new LoggerFactory();

Configure Microsoft.Extensions.Logging to use

factory.AddElmahIo("API_KEY", new Guid("LOG_ID"));

Finally, create a new logger and start logging exceptions:

var logger = factory.CreateLogger("MyLog");
logger.LogError(1, ex, "Unexpected error");

Filtering log messages

As default, the logger for Microsoft.Extensions.Logging only logs warnings, errors and fatals. The rationale behind this is that we build an error management system and really doesn't do much to support millions of debug messages from your code. Sometimes you may want to log non-exception messages, though. To do so, use filters in Microsoft.Extensions.Logging.

To log everything from log level Information and up, use the AddElmahIo overload which accepts a filter:

factory.AddElmahIo("API_KEY", new Guid("LOG_ID"), new FilterLoggerSettings
    {"", LogLevel.Information}

In the code sample, every log message with the category and a log level of Information and up, will be logged to To log a new information message, create a logger with the category and call the LogInformation method:

var logger = factory.CreateLogger("");
logger.LogInformation("This is an information message");

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.

Monitor your website