I can't get log4net to actually log anything to my database. First I added log4net to my project (MVC). Then I added this in AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
In Global.asax.cs Application_Start I end the method with
log4net.GlobalContext.Properties["ApplicationName"] = "MvcApp";
log.Debug("Application started");
I added the following model:
public class Log
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[Display(Name="Date")]
public DateTime Description { get; set; }
[Required, StringLength(255)]
[Display(Name="Thread")]
public string Thread { get; set;}
[Required, StringLength(50)]
[Display(Name = "Level")]
public string Level { get; set; }
[Required, StringLength(255)]
[Display(Name = "Logger")]
public string Logger { get; set; }
[Required, StringLength(4000)]
[Display(Name = "Message")]
public string Message { get; set; }
[Required, StringLength(2000)]
[Display(Name = "Exception")]
public string Exception { get; set; }
}
I added this to ApplicationDbContext:
public System.Data.Entity.DbSet<Models.Log> Logs { get; set; }
In web.config I added this in the configSections element:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
And then I created this as a child of the configuration element in web.config:
<log4net debug="true">
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Server=windflower.arvixe.com;Initial Catalog=coralys;User Id=coralysdb;Password=CoughUp_23;Application Name=CoralysInternet;" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="AdoNetAppender" />
</root>
But even with the log.Debug entries nothing is appended. I tried setting the buffer to 5 as a test but no result. I then added a method to flush the buffers as well, no result. And the level is set to maximum (DEBUG).
What else am I missing here?
It finally works. What helped was to enable Internal Debug on log4net and have it log to internal debug to a text file as described in
Log4net with Entity Framework 5 and MVC4
Having done that it turned out that the SQL statement was wrong, my log table had a different name and also by mistake I had named the Date column as Description. All those errors appeared in the internal debug.
Having fixed that, it worked.