Logging to Console using Log4Net

Logging can be very useful when debugging C# code. The two most popular log4net appenders are RollingFileAppender and ColoredConsoleAppender.

The RollingFileAppender builds on the FileAppender and can roll the log files based on size and date constraints. This organizes the log files, but reading through a lot of .txt files is not developer friendly.

However, ColoredConsoleAppender logs all messages to the console and highlights error, info, and debug messages with different colors, which makes the debugging experience a bit more pleasant.

Running a Website from a Command Line

To debug Episerver applications, we can:

  • Set up a website in IIS and attach the debugger to IIS.
  • Run a website in IIS Express by pressing F5 in Visual Studio.

We can also run the IIS Express from a command line, and this is where ColoredConsoleAppender comes into the play.

For more information about running the websites from a command line, see: https://docs.microsoft.com/en-us/iis/extensions/using-iis-express/running-iis-express-from-the-command-line

Let's say you've installed an Alloy sample site in C:\Projects\Alloy.

Open the applicationhost.config file in the .vs/config folder, and find the id of Alloy website.

It can be found somewhere around line 163.

My site section looks like the following:

<site name="Alloy" id="2">
	<application path="/" applicationPool="Clr4IntegratedAppPool">
		<virtualDirectory path="/" physicalPath="C:\Projects\Alloy\Alloy" />
	</application>
	<bindings>
		<binding protocol="http" bindingInformation="*:59039:localhost" />
	</bindings>
</site>

 To run a website from a command line, we need to create a .bat file, which executes iisexpress.exe, and points to correct config file, as follows:

cd "c:\Program Files\IIS Express"
iisexpress /config:C:\Projects\Alloy\.vs\config\applicationhost.config /siteid:2

Configuring ColoredConsoleAppender

To enable ColoredConsoleAppender, we must modify EPiServerLog.config.

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
   ...
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="[%thread] %level %logger: %message%n" />
      </layout>
   </appender>
   <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
         <level value="Fatal" />
         <foreColor value="White" />
         <backColor value="Red" />
      </mapping>
      <mapping>
         <level value="Error" />
         <foreColor value="White" />
         <backColor value="Red" />
      </mapping>
      <mapping>
         <level value="Warn" />
         <foreColor value="White" />
         <backColor value="Yellow" />
      </mapping>
      <mapping>
         <level value="Info" />
         <foreColor value="White" />
         <backColor value="Green" />
      </mapping>
      <mapping>
         <level value="Trace" />
         <foreColor value="White" />
         <backColor value="Purple" />
      </mapping>
      <mapping>
         <level value="Debug" />
         <foreColor value="White" />
         <backColor value="Blue" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%date [%thread] %level %logger: %message%n" />
      </layout>
   </appender>
   ...
   <logger name="Alloy">
      <level value="Debug" />
   </logger>
   <logger name="EPiServer">
      <level value="Debug" />
   </logger>
   <root>
      <level value="Error" />
	  ...
      <appender-ref ref="ColoredConsoleAppender" />
	  ...
   </root>
</log4net>

In this example, we’re enabling DEBUG log messages for Alloy and EPiServer assemblies.

Note: This is just for demonstration purposes. In the real world, you would most likely use a different configuration that generates less noise.

Running the Website

If we now run the website using a .bat file we previously created, we should get all log messages printed out to console. Debug messages are printed in blue, and info messages are printed in green.

IIS Express - Console Log Appender

comments powered by Disqus