There are three setup steps:

  1. Get log4net
  2. Create a table on Azure
  3. Update your web.config

Once these are done you can use your logger.

Get log4net

Use NuGet to get Log4Net.


Create a table on Azure

I created a table using the database management website – the database looks like this:


It’s really important that the ID column be an Identity column – Azure DB requires clustered indexes.

Update your web.config

Add these to your web.config or app.config

Add the config section entry:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

And then this section

 <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">  
   <bufferSize value="1" />  
   <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />  
   <connectionStringName value="DefaultConnection" />  
   <commandText value="INSERT INTO Log ([Date],[Level],[Message],[Exception]) VALUES (@log_date, @log_level, @message, @exception)" />  
     <parameterName value="@log_date"/>  
     <dbType value="DateTime"/>  
     <layout type="log4net.Layout.RawTimeStampLayout"/>  
     <parameterName value="@log_level"/>  
     <dbType value="String"/>  
     <size value="50"/>  
     <layout type="log4net.Layout.PatternLayout">  
     <conversionPattern value="%level"/>  
     <parameterName value="@message"/>  
     <dbType value="String"/>  
     <size value="4000"/>  
     <layout type="log4net.Layout.PatternLayout">  
     <conversionPattern value="%message"/>  
     <parameterName value="@exception"/>  
     <dbType value="String"/>  
     <size value="4000"/>  
     <layout type="log4net.Layout.ExceptionLayout"/>  

  <level value="All"/>  
  <appender-ref ref="ADONetAppender"/>  

Notice that the buffer size is set to 1 – this will flush after every log. Maybe change that to something larger when you go to scale.

Using the Logger

First initialize the logger. You can add this to whatever startup code you want (Global.asax.cs, OnStart, etc) …


You can now add a logger to your code like this:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And use it like this: