log4net自定义属性插入数据库无法正常工作

时间:2014-01-26 16:35:17

标签: c# log4net

我在 app.config

中添加了此代码
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender,log4net">
  <bufferSize value="1"/>
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  <connectionString value="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AccessTracesDatabase.mdf;Integrated Security=True"/>
  <commandText value="INSERT INTO AccessTraces ([SessionKey],[Date]) VALUES (@SessionKey,@Date)" />

  <parameter>
    <parameterName value="@SessionKey"/>
    <dbType value="String"/>
    <layout type="Log4NetTest.Logger.AccessTraceLogLayout, Log4NetTest">
      <param name="ConversionPattern" value="%property{SessionKey}"/>
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@Date"/>
    <dbType value="DateTime"/>
    <layout type="log4net.Layout.RawTimeStampLayout"/>
  </parameter>
</appender>
<logger name="AccessLogger">
  <level value="ALL" />
  <appender-ref ref="ADONetAppender" />
</logger>

我创建了三个类:

AccessTraces.cs

public class AccessTraces
{
    [Key]
    public string SessionKey { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public DateTime Date { get; set; }
}

AccessTraceLogLayout.cs

public class AccessTraceLogLayout : PatternLayout
{
    public AccessTraceLogLayout()
    {
        AddConverter("property", typeof(AccessTraceLogMessagePatternConverter));
    }
}

AccessTraceLogMessagePatternConverter

public class AccessTraceLogMessagePatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        if (Option != null)
        {
            // Write the value for the specified key
            WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
        }
        else
        {
            // Write all the key value pairs
            WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
        }
    }

    private object LookupProperty(string property, LoggingEvent loggingEvent)
    {
        object propertyValue = string.Empty;

        var propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
        if (propertyInfo == null) return propertyValue;
        propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);
        return propertyValue;
    }
}

最后,我使用实体框架创建了一个数据库。

AccessTracesContext.cs

public class AccessTracesContext : DbContext
{
    public AccessTracesContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<AccessTraces> Accesses { get; set; }
}

和连接字符串:

<connectionStrings>
    <add name="DefaultConnection"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AccessTracesDatabase.mdf;Integrated Security=True" />

所有他们都投入项目 Log4NetTest

private static readonly ILog AccessLogger = LogManager.GetLogger("AccessLogger");
AccessLogger.Debug(new AccessTraces
{
    SessionKey = DateTime.Now.Minute.ToString(CultureInfo.InvariantCulture),
    Date = DateTime.Now
});

另外,我用:

XmlConfigurator.Configure();

在应用程序的入口点。

但是,日志无法插入数据库。谁能告诉我为什么?

0 个答案:

没有答案