无法使用Log4Net插入到MSSQL表中

时间:2011-10-09 02:30:15

标签: sql-server-2008 log4net

我正在测试Log4Net,并且能够写入文本文件,事件查看器。我试图用MS SQL 2008 Express测试它,我似乎无法将其插入到我在本地创建的数据库中。

实例:(本地)

数据库名称:Log4Net

我在下面的配置中省略了连接字符串上的用户ID和密码,因为我在本地运行它:

<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>

这是测试C#代码:

 if (log.IsDebugEnabled)
                {
                    log.Debug("Insert some useful comment..");
                }

                if (log.IsErrorEnabled)
                {
                    log.Error("Some Error blah blah.. Testing Only..");
                }

P.S。 我肯定已经创建了“Log”表。

链接到配置示例:http://logging.apache.org/log4net/release/config-examples.html

提前致谢

1 个答案:

答案 0 :(得分:1)

如果要从连接字符串中省略用户ID和密码,则需要设置integrated security=true