无法使用log4net AdoNetAppender插入到SQL Server表中

我试图了解log4net是如何工作的,所以我已经将它添加到我的 app.config (我应该添加控制台Appender和FileAppender完美地工作,我只是在使用AdoNetAppender时遇到问题) .

我该如何调试它,看看至少与db的连接是否成功?

问题是INSERT语句没有执行 . 我应该加上

Data Source=MyWorkgroup\SQLEXPRESS;Initial Catalog=MyNewDatabase;User ID=juan;password=juan,

当我尝试手动连接到SQL Server时工作正常,因此不确定是否存在问题 .

此外, ConnectionType 取自官方网站:https://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Appender.AdoNetAppender.ConnectionType.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" 
                 type="log4net.Config.log4netConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
        <appender name="AdoNetAppender" 
                  type="log4net.Appender.AdoNetAppender">
            <bufferSize value="10" />
            <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <connectionString value="Data Source=MyWorkgroup\SQLEXPRESS;Initial Catalog=MyNewDatabase;User ID=juan;Password=juan;Pooling=False" />
            <commandText value="INSERT INTO Logs([logDate],[logThread],[logMessage]) VALUES(getdate(),'1','1')" />
            <commandType value="Text" />
        </appender>
        <root>
            <level value="DEBUG"/>
            <appender-ref ref="AdoNetAppender"/>
        </root>
    </log4net>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

稍后编辑(使用David推荐的调试方法后):

haacked.com确实非常有趣,很棒!但是,似乎它正在描述我要指示log4net记录的内容,而不是这些操作的结果(?),如果我读得很好(没有失败/或成功?),例如

log4net:将属性[ConnectionType]设置为String值[System.Data.SqlClien t.SqlConnection,System.Data,Version = 1.0.3300.0,Culture = neutral,PublicKeyToke n = b77a5c561934e089] log4net:将Property [ConnectionString]设置为String值[数据源= MyWorkgroup \ SQLEXPRESS;初始目录= MyNewDatabase;用户名= juan;密码=娟; Pooling = False] log4net:将Property [CommandText]设置为String值[INSERT INTO Logs([logDate],[logThread],[logMessage])VALUES(getdate(),'1','1')] log4net:设置属性[CommandType] to CommandType value [Text] log4net:Created Appender [AdoNetAppender] log4net:将名为[AdoNetAppender]的appender添加到logger [root] . log4net:层次结构阈值[]

帮助我获取此信息的代码段(如果站点不可用)是:

<appSettings>    
      <add key="log4net.Internal.Debug" value="true"/>
 </appSettings>

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
          name="textWriterTraceListener"
          type="System.Diagnostics.TextWriterTraceListener"
          initializeData="C:\temp\log4netdiagn.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

回答(0)