log4net不会将日志写入SQL Server

时间:2016-10-18 08:13:25

标签: c# asp.net sql-server log4net

我正在尝试使用log4net登录到我的数据库中。我已经使用NuGet安装了log4net,并设置了web.config,如下所示:

<configSections>    
    <!-- Log4Net -->
    <section name="log4net" 
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
    <!--AdoNet appender is use for write log file into SQL Server-->
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="data source=localhost/SVILUPPO;Initial Catalog=MIODB;user id=sa;password=myPassword;" />
        <commandText value="INSERT INTO AppLog ([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>

<!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->

    <root>
        <level value="ALL" />
        <appender-ref ref="AdoNetAppender" />
        <!--Enable this line if you want write log file into sql server-->
    </root>
</log4net>

然后我在global.asax中设置了对配置的调用:

log4net.Config.XmlConfigurator.Configure();

这在我想要的页面中获取并保存日志

private static ILog log = LogManager.GetLogger(typeof(Login));

log.Debug("Debug message");
log.Warn("Warn message");
log.Error("Error message");
log.Fatal("Fatal message");

但没有任何反应:(

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

要查找错误,您最好在log4net中启用内部调试。 log4net中的错误不会发送到您的应用程序。您可以将其写入日志,然后查看您的日志记录无法正常工作的原因。

在你的app.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

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

Log4net FAQ