Setting up log4net in ASP.NET MVC
I seem to burn far to many hours trying to do the relatively simple task of setting up log4net on a project. Well step one is easy enough, install through nuget. Next...
Don't forget to set the log4net.config file to Copy Local in visual studio properties window.
Finally if you are using the Visual Studio output window, its worth knowing that you can right click and turn off the white noise:
Add web.config settings
I like to create a separate config file for log4net because it keeps things simpler. So add the following config:Add the log4net config file
Next add a file called log4net to your solution and add the following config. This is a good starting point, logging to a file and also to the console. There is ALOT of config options for log4net, but this is a good starting point:Don't forget to set the log4net.config file to Copy Local in visual studio properties window.
Initalise the xml config
This is the bit I always forget. One line of code you stick somewhere in your app startup to get log4net to initialise using the xml configuration we just added;Write log statements
Now we are free to make full use of the log4net logging capabilities. First declare a logger at the top of the class, then log away:Finally if you are using the Visual Studio output window, its worth knowing that you can right click and turn off the white noise: