Setting up a New MVC 6 Project with Serilog, DryIoc, and xUnit.net

I am creating a new template project that implements a number of new technologies and design patterns that I have been wanting to learn more about and will be sharing want I learn along the way.

Serilog

The first thing I wanted to do was setup Serilog in a new ASP.NET MVC 6 project. I came across this article on the companies blog that walks you through installing and configuring Serilog in an ASP.NET Core project. Since ASP.NET Core projects use the same Startup.cs file structure I was able to follow the instructions and got Serilog up and running without issue.

DryIoc

The next item I wanted to configure was dependency injection. MVC 6 makes this much simpler than in the past as dependency injection is built into the core. I have been a long time user and fan of StructureMap, but I wanted to see what else might be available today. I found two very informative sites including this site with detailed benchmarks and feature comparisons and this site that dives even deeper into comparing the different frameworks feature support. The standout on both sites was DryIoc with the fastest test times and scoring 100% on the feature tests.

Configuring DryIoc in the Startup.cs took a little longer than I was hoping because the documentation didn’t cover MVC 6 and Google didn’t hit on a lot of articles for DryIoc. Documentation and available information is something to consider when choosing any framework. None the less I found the package I needed DryIoc.AspNetCore.DependencyInjection and the three lines of code that got DryIoc up and running.

https://gist.github.com/JoshuaCode/51844021a6f9cbdce91976c13f9a9efc

Here is the complete Startup.cs file with Serilog and DryIoc.

https://gist.github.com/JoshuaCode/02b829fe30a57a79d61e4a9a3e4dabcd

xUnit.net

Once I had logging and dependency injection configured it was time to write some tests. xUnit’s documentation is great so I recommend you start there. The two points that I got stuck on after getting xUnit set up was how to pass an instance of ILogger to my MVC controller and once I do actually see the console output. Patrick Smith a co-worker of mine helped me solve the first issue. Here is the controller code and the test class.

https://gist.github.com/JoshuaCode/dfc0792acd9d43e5123ea8dd1576c4b7

https://gist.github.com/JoshuaCode/38281270bb9cd52bb44143662afa0dbf

To see the logger output I found serilog-sinks-xunittestoutput which writes to the xUnit output stream and displays it with your test results as you see below.

I hope this helps others get started with these tools and I will continue to post as I continue to develop this project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s