I am testing Log4Net and have been able to write to text files, event viewers. I was trying to test it with MS SQL 2008 Express and I cannot seem to get it to insert into the DB I have created locally.
Instance: (local)
DB Name: Log4Net
I have开发者_JAVA百科 omitted the User Id and Password on the connection string in the config below as I am running it locally:
<log4net debug="true">
<!--
APPENDING TO A TEXT FILE
-->
<!--<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Log4Net\\TestLog.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>-->
<!--
WRITING TO THE EVENT LOG
-->
<!--<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Log4Net Test" />
<layout type="log4net.Layout.PatternLayout">
--><!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />--><!--
<conversionPattern value="%date - %message%newline" />
</layout>
</appender>-->
<!--
WRITING TO MS SQL
-->
<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=(local);initial catalog=Log4Net;integrated security=false;persist security info=True" />
<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>
</log4net>
Here is the test C# code:
if (log.IsDebugEnabled)
{
log.Debug("Insert some useful comment..");
}
if (log.IsErrorEnabled)
{
log.Error("Some Error blah blah.. Testing Only..");
}
P.S. I definitely have created the 'Log' table already.
Link to config examples: http://logging.apache.org/log4net/release/config-examples.html
Thanks in advance
If you want to omit user id and password from the connection string, you need to set integrated security=true
.
精彩评论