Things I'm likely to forget.

It doesn't matter if your API is nothing more than a facade for simple CRUD operations built on-top of an Active Record implementation or if your API is merely the gateway into your complex Domain Driven Design that leverages the latest and greatest CQSR/ES patterns to scale on demand: you need to validate your input.

Writing code to validate input is quite possibly one of the most tedious task ever created (right next to doing any sort of processing that involves any file format from the medical world). Thankfully we are far from the days of having to manually handle that task.

When I started my latest ASP.NET Core 1.1 project I wanted a more expressive way to handle validation. Enter FluentValidation: a small library that does an excellent job handling input validation (high level validation before you get into the heart of your business logic). Below I show you the three phases my validation code went through before I finally end up where I probably should have started.

Getting Started

Before getting to far into this tutorial you'll want to make sure you:

The First Pass (Also Known as the Really Bad Idea)

I knew I wanted to use FluentValidation and I know that ASP.Net has built in model validation. What I didn't know was how to bring them together. Eventually I got there, but it took a few passes. The first step was a given, add the required dependencies to my project.json file:

With that in place I wrote a few validators for the input into my POST and PUT action handlers. You can find all of the documentation for FluentValidationhere. Below is an example command and it's related validator:

Next, I wanted a generic way to get the validator for a given class based on the class's type. I didn't want to have to know what the exact type of the validator implementation was. This seemed like a good time to use StructureMap (in reality, it wasn't). Here's how you could use StructureMap if you really really wanted to (for some reason):

In your StructureMap scanner configuraiton add the following line:

s.ConnectImplementationsToTypesClosing(typeof(AbstractValidator<>));

ConnectImplementationsToTypesClosing will allow you to get an instance of a given validator knowing only the type of the object the validator handles. I wanted to be able to get an instance of a validator using the same syntax I would use to get any other object from my IoC container. While I generally avoid extension methods, this seems like a good place to put the pattern to use:

That's, ummm.... not good. There are a few major problems with this solution:

It requires injecting my IoC container into the controller (leaky abstraction, I might as well have my repositories return IQueryable while I'm at it)

I have to type the same few lines at the beginning of every method where I want to validate the input

The Second (Less Bad) Solution

Surface level validation is kind of a cross-cutting concern right? Aspect Oriented Programming is a way to handle cross-cutting concerns... AOP frequently uses attributes... I can use an attribute to handle my validation. A few degrees short of Kevin Bacon and I have a new direction: ValidateInputAttribute:

Unfortunately I still had one problem: I can't inject my logging framework of choice into an Attribute. Well, where there's a will there's a way... but if you have to work that hard to get something to work, there's probably a better solution...

The Third and Final Attempt (for Now)

Filters allow you to execute code in the MVC Action Pipeline prior to the action being executed but after the model had been bound via Action Filters. This seems like the perfect place to solve my problem.

Before we can handle validation errors in a Filter we first need to update our Startup class's ConfigureServices method:

The highlighted lines above will configure things so that FluentValidation handles validating input for you and updates the ModelState accordingly. Note, using this approach does not require StructureMap.

Next you need to add an implementation of IActionFilter to handle validation errors:

The above code should be pretty straight forward. If there are no errors in the given model, simply return and do nothing. Let the next filter do it's thing. If there is a problem with the ModelState, log it and let the client know that a Bad Request was made (HTTP status code 400).

Note, in the current version of FluentValidation if a null object is passed into your action method context.ModelState.IsValid will return true. Given what I read here, that's not what I expected.

Conclusion

I double my current solution is perfect and I'm almost certain it'll go through another refactoring (or two) as I continue to work on the project. Hopefully you found something useful above. If not, thanks for taking the time to read this article and I would appreciate any feedback you might have.

Why use Serilog over NLog

For the longest time I didn't understand why everyone was so excited about Serilog. I've used NLog for a long time and it seemed more than capable of doing what I needed: logging messages to some data store (log files, databases, etc...).

Then I stared using Elasticsearch. Suddenly I saw the light. Structured event data always struck me as one of those neat features that wasn't really needed. However, once you start using something like Elasticsearch the power of structured event data quickly become evident.

Adding the visualizations offered by Kibana takes your logging to the next level. A quick glance at a dashboard and you instantly know if there has been an uptick in errors (or whatever you might be logging). The interactive visualizations allow you to quickly filter out noise and identify the root cause of problems you or your users might be experiencing.

Installing the JDK

This part is frequently overlooked. You need to have Java installed and running on the box you're planning to use as your Elasticsearch server. For the purposes of this tutorial I'm going to assume that you're going to set things up on a Windows machine. You'll also need to ensure that the JAVA_HOME environment variable is correctly set.

Step 1: Download the JDK

You can download the current JDK from Oracle. You'll want to click on the "Java Platform (JDK) 8u111 / 8u112" link. On the following page download the appropriate package (in my case it's jdk-8u111-windows-x64.exe). Once the download completes run the installer and let it do it's thing. In most cases the default options (install location, etc...) are just fine.

Step 2: Setting the JAVA_HOME Environment Variable

In order for Elasticsearch to work you'll need to have the JAVA_HOME variable set. In order to set the JAVA_HOME variable you'll need to access the Advanced System Settings in Windows. You can do that by:

Click on the Start Menu

Right click on Computer

Select Properties

In the window that appears, select Advanced System Settings in the upper left. That will bring up a small dialog window with five tabs across the top:

Select Advanced.

Click on Environment Variables...

In the System variables section click New...

For the Variable Name use JAVA_HOME and for the value use the location of your install's bin directory

Setting up Elasticsearch and Kibana

I'm not going to teach you the ins and outs of Elasticsearch. I'm going to give you just enough information to get things up and running. Elasticsearch is incredibly powerful and I strongly encourage you to get a book or consult with someone more knowledgeable than me.

Step 1: Download and Install Elasticsearch

Elastic does a great job walking you through the steps necessary to setup Elasticsearch on any platform. Rather than repeating that information here, I'll simply point you in the right direction: https://www.elastic.co/downloads/elasticsearch.

Although the download page has some basic installation instructions, I found the instruction in the documentation to be much more helpful. You can find that information here.

I would strongly recommend setting up Elasticsearch as a service. However, you'll want to make sure you have Elasticsearch successfully running before setting up the service. It's much easier to resolve problems when you can see the errors in the console window.

Step 2: Download and Install Kibana

Installing Kibana goes pretty much exactly like installing Elasticsearch. Simply download the compressed file, decompress to wherever you like, then run the bat file. You can download Kibana from here: https://www.elastic.co/downloads/kibana. You can find better installation documentation here.

If you want to setup Kibana to run as a service you can use the following command in the Windows Console or your preferred terminal (you can see my setup here):

At this point you should be able to verify that Elasticsearch is running at http://localhost:9200 and that Kibana is running at http://localhost:5601 by visiting those URLs in your preferred browser.

Using Serilog

As mentioned in the introduction, we'll be using Serilog instead of NLog. This is so that we can take advantage of the structured data Serilog gives us in our Elasticsearch indexes. Setting up Serilog with .NET Core is pretty straight forward.

Looking at the code you should notice that loading settings from my appsettings.json file. If you need some help with that you read my previous post.

I've enriched my events in two ways. First I've configured Serilog to use the LogContext. For more information take a look at the Serilog documentation here. The second enrichment simply puts the application name on every event generated by Serilog.

I always want Serilog to write to the console (at least while the application is being developed). To accomplish that I'm using the LiterateConsole Sink. If you want to know why I'm using the LiterateConsole over the ColoredConsole you can read more about it here.

Lastly, depending on the value in esConfig.Enabled I'm conditionally setting up the Elasticsearch sink. You can find all the information about the various configuration options here. Here is the short version:

AutoRegisterTemplate - Tells Serilog to automatically register a template for the indexes it creates using a template optimized for working with Serilog events.

MinimumLogEventLevel - Kind of straight forward.

CustomFormatter - In order to avoid deeply nested objects Serilog writes inner exceptions as an array of exceptions. This can be problematic for visualizations and some queries. You can change this behavior using the ExceptionAsJsonObjectFormatter.

IndexFormat - This is the pattern Serilog will use to generate the indexes it creates. Typically it's something like "api-logs-{0:yyyy.MM.dd}". If you do not provide a format Serilog will use it's default value.

That's it. Now you're ready to start writing some events to Elasticsearch.

Step 3: Write some exceptions to Elasticsearch

You'll need to use Serilog's ILogger interface wherever you need to log an event. I tend to use StructureMap as my IoC container instead of the default implementation Microsoft offers. This means I need to register the interface in my StructureMap configuration:

_.For<Serilog.ILogger>().Use(Log.Logger);

Once that is done, I can easily inject Serilog into any object created via the IoC container (i.e. my controllers). Writing an event with structured data to Elasticsearch is as simple as making the following call in your code wherever appropriate:

For more information about the features Serilog offers please refer to their documentation. I encourage you to take advantage of source contexts whenever possible. Having the SourceContext property in your event data makes filtering a lot easier.

Using Kibana

It's taken a while, but you've finally got Elasticsearch setup, Kibana installed and running, and your source code writing events to an Elasticsearch index. Great... now it's time to start seeing the effort pay off.

Step 1: Setup Your Index Pattern

If this is the first time you've run Kibana you will most likely be looking at the screen where Kibana asks you to tell it about your index pattern:

If you recall, back when we setup the Serilog Elasticsearch sink one of the properties we configured was the IndexFormat. This is the value you'll want to use here less the date format portion of the string. If you used "api-logs-{0:yyyy.MM.dd}" for your IndexFormat, then the Index Pattern is "api-logs-".

With the Index Pattern set you'll want to head over to the Discover tab.

Step 2: Save a Simple Query

Before you can discover anything you'll need to make sure you've logged at least a few events to Elasticsearch. You'll also want to make sure that they occurred within the time frame you're currently viewing (look in the upper right corner of the Kibana window). As long as you have some events stored in ES, clicking on Discover should display a window that looks something like this:

In order to create a visualization you're going to need to save a search. You can find the full Discover documentation here. For the purposes of moving forward, we'll save a simple search:

In the Query bar type in: level:Error

Click on the search button (magnifying glass)

Click on Save in the upper right corner

Give the search a slick name like All Errors

Click Save

Step 3: Create a Simple Visualization

With the search saved it's time to move over to the Visualize section of Kibana.

There are several visualizations you can create. In this example we'll create a simple Vertical Bar Chart using a Date Histogram to group the errors by date and time. Creating this visualization is pretty straight forward:

Select the Vertical Bar Chart option from the list of visualizations

On the right you can select All Errors from the list of saved searches

In the next window select X-Axis

Under Aggregation choose Date Histogram

Leave all of the default settings

Click on the run button (Looks like a play button)

Click Save in the upper right

Step 4: Build your dashboard

With the visualization saved you can easily add it to your dashboard. You can find a lot more information about building dashboards than I can find in the official Kibana documentation.