我无法让 log4net 将任何内容实际记录到我的数据库中。首先,我将 log4net 添加到我的项目(MVC)中。然后我在 AssemblyInfo.cs 中添加了这个
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
在 Global.asax.cs Application_Start 中,我以
log4net.GlobalContext.Properties["ApplicationName"] = "MvcApp";
log.Debug("Application started");
我添加了以下模型:
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; }
}
我将此添加到 ApplicationDbContext:
public System.Data.Entity.DbSet<Models.Log> Logs { get; set; }
在 web.config 中,我在 configSections 元素中添加了这个:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
然后我将它创建为 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>
但即使有 log.Debug 条目,也没有附加任何内容。我尝试将缓冲区设置为 5 作为测试,但没有结果。然后我添加了一个方法来刷新缓冲区,没有结果。并且级别设置为最大值(DEBUG)。
我在这里还缺少什么?