Log4net has quickly become the de facto logging package for many C# and VB web development packages. You'd think, because of that, the setup and configuration would be seamless. Install and BOOM! You're up and running. It's not that easy, however.
In this blog we will install and configure log4net for a MVC project.
Start with installing the package. Within Visual Studio, click Tools, NuGet Package Manager, and Manage NuGet Pacakges for Solution. Click on Browse and search for log4net. At this time, this is the latest package.
Install it to your MVC application (and whatever other projects might use it). Close out of the package manager.
Now, add the configuration file. In the web project, add a file called Log4Net.config and paste the following into the file.
- <log4net>
- <root>
- <level value="ALL" />
- <appender-ref ref="DebugAppender"/>
- </root>
- <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline" />
- </layout>
- </appender>
- </log4net>
Next, let's hook up the file the XSD schema.
- While in the log4net.config file, click on XML across the top of the screen,
- Select Schemas,
- Find DotNetConfig.XSD and click the Use checkbox in column 1 as shown below.
Click Ok to save your changes. All this does is avoid a compiler warning, but it is certainly valuable in configuration.
Next, make sure the log4net.config file is always copied to the output folder. Right click on log4net.config file and then select Properties. Change the Copy to Output Directory to Copy Always as shown below.
Next thing you need to do is setup your web.config file to use the log4net configuration file and configure the section. To do that, open your web.config and paste in the following highlighted lines. These lines are added directly below the <configuration> tag.
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
- </configSections>
- <log4net configSource="log4net.config"/>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework, Version=v4.5"/>
- </startup>
Finally, in the AssemblyInfo.cs file you need to add the following line at the bottom of the document.
-
-
-
-
-
-
-
-
-
- [assembly: AssemblyVersion("1.0.0.0")]
- [assembly: AssemblyFileVersion("1.0.0.0")]
- [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config")]
Now, your project is configured for log4net so let's use it to create a log message.
In your HomeController, add the following code:
- private static readonly log4net.ILog log = log4net.LogManager.GetLogger("DebugAppender");
-
- public ActionResult Index()
- {
- log.Info("Hello world!");
- return View();
- }
When you run your project, the logging line above will render values in the Output window as shown below.
Of course, there is a world of other possibilities in terms of logging. The destination of the messages (file, console, database, etc.) as well as the logging levels (debug, info, warning, error, critical, etc.) are all configurable, but that's for a different blog.
Log4net is certainly powerful and a valuable tool for any application.
Enjoy!