log4net写文件是好的,但不能插入到数据库
这是我的配置文件包含log4net属性,如果我使用“RollingLogFileAppender”appender它正确写入文件,但我使用 “AdoNetAppender”,并期望在消息列中看到相同的字符串的插入行。但不知何故,这是行不通的。log4net写文件是好的,但不能插入到数据库
(表定义和查询配置是来自apache文档,所以它不能错我虽然连接字符串是我的问题,但应用程序已经有连接相同的数据库我也共享连接字符串是通过ADONET正在使用)
<log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\AkisLog\\Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=******\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd" />
<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="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
...
<connectionStrings>
<add name="TedasDbContext" connectionString="metadata=res://*/Project.csdl|res://*/Project.ssdl|res://*/Project.msl;provider=System.Data.SqlClient;provider connection string="data source=****\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
和cs文件:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); if (log.IsErrorEnabled)
{
log.Error("Error ocurred here");
}
if (log.IsDebugEnabled)
{
log.Debug("Debug message");
}
回答:
你的缓冲区大小是100,这意味着消息将后100条消息被写入。将其更改为1,这将直接刷新消息发送到SQL数据库:
<bufferSize value="1" />
如果不工作,使log4net的INTERAL调试,看看你是否有连接问题。同时检查你是否有最新版本的log4net。
以上是 log4net写文件是好的,但不能插入到数据库 的全部内容, 来源链接: utcz.com/qa/262423.html