我在使用 Log4Net 登录到我的数据库时遇到问题。
我不太确定我的结果是什么。当我应该获取字段的值时,我一直在获取字段名称。
该程序是一个 SystemTray 实用程序。我已将 Log4Net 设置如下。
在大会中:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
在 Program.cs 中:
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
[STAThread]
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
在调用类中:
public class HostManager
{
#region constants
protected static readonly ILog log = LogManager.GetLogger(typeof(HostManager));
...
// Inside an event method:
try
{
log.Info(new Log
{
CreateDate = DateTime.Now.ToString(),
SystemUpTime = FormatUpTime(_states.TimeFromStart),
EmotivType = "boredom",
EmotivValue = _states.AffectivEngagementBoredomScore.ToString(),
EventTimer = _eventTimer,
SubEventTimer = _subEventTimer
});
}
Log 类只是一堆字符串类型的 getter 和 setter。
而且,我的 app.config 文件带有 log4net 设置:
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="Data Source=localhost; Initial Catalog=[MyDatabase]; Integrated Security=True;"/>
<commandText value="INSERT INTO Session ([CreateDate], [SystemUpTime], [EmotivType], [EmotivValue], [EventTimer], [SubEventTimer])
VALUES (@createdate, @systemuptime, @emotivtype, @emotivvalue, @eventtimer, @subeventtimer)"/>
<parameter>
<parameterName value="@createdate"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@systemuptime"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="@emotivtype" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%emotivtype" />
</layout>
</parameter>
<parameter>
<parameterName value="@emotivvalue"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%emotivvalue"/>
</layout>
</parameter>
<parameter>
<parameterName value="@eventtimer"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="@subeventtimer"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
结果是 %emotivtype 和 %emotivalue 总是收到字段名称:
ID CreateDate SystemUpTime EmotivType EmotivValue EventTimer SubEventTimer
13547 17:06:56.787 17:06:56.787 emotivtype emotivvalue 17:06:56.787 17:06:56.787
这似乎是一件愚蠢的、可以修复的事情,但它让我无法理解。有什么建议么?谢谢。