NLog File Archiving Gotcha

When configuring NLog for your .Net application, you will most probably want to setup some sort of log file archiving features – that’s if you’re using a file based target. Checkout that link if you need some help on how to do this.

If you’re developing an ASP.NET MVC application, you can easily install NLog via NuGet. Be sure to search for NLog Configuration which will install a sample config file for you, along with the NLog library with the core functionality.


Here’s a sample of the default config file that you get from the NuGet package:

This is my slightly customised version:

  • I’ve changed the log file location to be in a directory under C:\logs.
  • Customised the message layout to include such things as the Thread ID, Class name where the message originates and the exception format.
  • This exception format is actually very important. If you don’t explicitly specify the formatter, any exceptions you log will not appear in your log file (or other logging target).
  • Lastly, I’ve added archiving configuration attributes to perform time based file rollover.

This code will work nicely if you’re running your webapp from within Visual Studio. Each day, NLog will rollover the previous days log file, giving it a new name based on yesterdays date:


If, however, you have configured your webapp to be accessible via IIS rather than IIS Express in Visual Studio, you will discover that the log file archiving feature won’t work! What the…!!?? This stumped me for days until I pulled out Sysinternals Process Monitor in the hope it would help me figure things out… and it did. It turns out there was a permissions error and the IIS App Pool process was not allowed to create the new archive file and roll over a new log file for the current day. So I added the right permissions to the C:\logs directory for the App Pool, but that still didn’t work. That’s when I noticed by chance, that in my NLog config file, I only had a relative path specified for the archiveFileName:

instead of the fully qualified path like this:

I then re-ran the webapp and low and behold, the archiving feature worked!

So why then does it work when you run the app from within Visual Studio with just the archive filename declared but not from under the full IIS process?? I actually don’t know. I figured it might be a possible bug, or more likely something related to User Profile loading under IIS and the resulting permissions that are required for the file rollover not being available. But this is definitely a big gotcha if you’re not careful.

StructureMap.MVC5 Errors in Default Visual Studio 2013 MVC WebApp

StructureMap is the leading Dependency Injection/Inversion of Control (IoC) library for .Net development. So of course as I started a brand new project it was one of the very first NuGet packages I installed.

Just as an FYI… if you’re doing ASP.NET MVC development, you’ll want to install the newer StructureMap.MVC5 package, and if you’re adding Web API functionality to your project, you’ll want to use StructureMap.WebApi2 – which will install StructureMap.MVC5 as a dependency.

I had created a Web Application with authentication using the ‘Individual User Accounts’ option enabled. I ran the app prior to installing StructureMap just to make sure it compiled and all was good:


I then clicked around the pages, checking everything was ok. It all worked fine. I then right clicked my solution node in the solution explorer and selected ‘Manage NuGet Packages for Solution…’


On the left side of the Package Manager dialog click and select to view ‘Online’ packages. In the upper right corner type into the search box ‘StructureMap.MVC5′ and hit enter to start searching. You should see StructureMap.MVC5 come up in the search results in the middle column.


Now click the ‘Install’ button and you will see the dialog prompting which projects in the solution to install the package. Select the ones you want and then click the ‘OK’ button.










You will see a progress window appear as NuGet installs the package and a new green tick will appear next to the StructureMap.MVC5 item in the search results.



Now recompile and run the webapp again. This time click on the ‘Register’ link and see what happens.



You will see an unusual error message displayed in Visual Studio:

An exception of type ‘Microsoft.Practices.ServiceLocation.ActivationException’ occurred in Microsoft.Practices.ServiceLocation.dll but was not handled in user code

Click the link that says ‘Copy exception detail to the clipboard’ and then paste it into your favourte text editor. This will give us a hint as to what is going wrong.


You can see that the error occurred while trying to instantiate the AccountController class. This is the controller that manages all the account creation, registration and login activities. Line 11 gives us some more information that points to the root cause.

No default Instance is registered and cannot be automatically determined for type ‘IUserStore<ApplicationUser>’

This is because StructureMap doesn’t know which concrete class to instantiate when it sees an Interface of type IUserStore<ApplicationUser>. If you’re like me (but you’re probably not – you’re a whole lot smarter) you’ll think, that’s fine, I’ll just add a mapping entry to the DefaultRegistry.cs file:


But when you actually try to think what concrete implementation class to provide, you’ll be left scratching your head!

The key to remember here, is that StructureMap is ‘greedy’ about which constructor it will try to call, as it creates concrete instances of each interface. So it’s trying to create an instance of the AccountController class that requires 2 parameters.


Unless we tell it to use a difference constructor, or we figure out all the concrete classes to register in the StructureMap registry. For more information on this checkout this Stackoverflow post on the topic.

The easiest way to solve this error, is to tell StructureMap which constructor to use. In other words, we want to satisfy the error message which asks for a default constructor. Thankfully there is a nice and simple way to do this. Simply add the [DefaultConstructor] attribute to the default, no arg constructor in AccountController.


StructureMap will see this ‘hint’ and use this constructor and all will be fine. You can click the Register or Login links without any exceptions being thrown.

Now, if you don’t like the feel of skipping out on the principle of IoC with the ASP.NET Identity classes, you can register the various concrete classes that are required to let StructureMap invoke it’s preferred constructor – the one with 2 parameters!


We need to register 3 concrete implementations in our registry. I must confess, I got 2/3rds of the way there, but I couldn’t manage to figure out how to register the Owin Authentication Context. Credit for this solution goes to Pouya Yousefi who answered this StackOverflow question. He links to a nice code example on Github too.

Hopefully this helps anyone else who is struggling with their StructureMap config in an ASP.NET MVC web app.


Want to Self-Publish a Super Successful Ebook?

“I worked really, really long hours. Think 12-hour workdays, with around 8 hours of working on the book and around 4 hours of consulting work to keep the money coming in,” says Samuel. It took him around three months to finish The Elements of User Onboarding.

This is a great review on Nathan Barry’s blog about how Samuel Hulick wrote and shipped his ebook to huge success!

Windows 8.1 Install Fails on VMware Fusion 6.0.2

If you are trying to install Windows 8.1 into a virtual machine using VMware Fusion v6.0.2 and you get the following error, it might be that your CD/DVD ROM settings are incorrect.

Missing Driver File

Missing Driver File

Some websites will tell you that you have a corrupted .iso file. This might well be the case, but if you have checked the checksum of the downloaded file, that’s obviously not your problem. What the message is telling you is that it needs a driver to be able to control the CD/DVD drive correctly. This could simply be a config error in your VM.

Instead, check to see what your CD/DVD ROM settings are for your virtual machine. Cancel out of the Windows Setup screens and shutdown your VM. You can still keep the Windows install image connected to your CD/DVD drive.

Click the “Edit Hardware Settings” button for the VM:

Edit VM Settings

Edit VM Settings Button

This will open a new Settings dialog window. next click on the “CD/DVD” icon under the “Removable Devices” section:

VM Settings

VM Settings

Notice how mine says (SCSI)? This is a hint that your settings might be wrong. Expand the “Advanced Options” on the next screen to see what Bus Type your VM is set to use:

SCSI Bus Type

SCSI Bus Type

If it’s SCSI change it to SATA:

SATA Bus Type

SATA Bus Type

Next click the “Show All” button – this will take you back to the main Settings window. Click on the “Startup Disk” item:

Settings - Startup Disk

Settings – Startup Disk

Select the CD/DVD icon and then click the “Restart” button:

Startup Disk - Restart

Startup Disk – Restart

This will reboot your VM and your Windows 8.1 installation should continue successfully.


Record Solar Output

Today I hit 39.0kWh of power generation. I’m really looking forward to seeing what the long days of summer will bring!! Here in Perth, Western Australia, we get so many gorgeous sunny days without a cloud in the sky. Perfect for solar generation.

Last 2.5 months of daily output:


Todays generation graph: