Aspnetboilerplate:无法将日志写入SQL Server

时间:2018-11-30 09:50:48

标签: c# asp.net log4net aspnetboilerplate

我想将日志写入SQL Server而不是文件中。我使用方法Logger.Debug(),但它没有将任何内容写入SQL Server。

我已经在数据库中创建了一个表HtcLog:

CREATE TABLE [dbo].[HtcLog] 
(
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [Date] [datetime] NOT NULL ,
    [Thread] [nvarchar] (255) NULL ,
    [Level] [nvarchar] (50) NOT NULL ,
    [Logger] [nvarchar] (255) NULL ,
    [Message] [nvarchar] (4000) NOT NULL , 
    [Exception] [nvarchar] (2000) NULL , 
)

我对log4net.config进行了如下修改:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>      
  <appender name="AdoNetAppender_SQLServer" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" />
    <connectionType value="System.Data.SqlClient.SqlConnection,System.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=192.168.50.60;initial catalog=test_db;integrated security=false;persist security info=True;User ID=sa;Password=123456" />
    <commandText value="INSERT INTO HtcLog ([Date],[Thread],[Level],[Logger],[Message],[Exception], [CreationTime]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @log_date)" />
    <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="%p" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%c" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="4000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%m" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@exception" />
      <dbType value="String" />
      <size value="2000" />
      <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
  </appender>
  <root>
    <appender-ref ref="AdoNetAppender_SQLServer" />
    <level value="DEBUG" />
  </root>
</log4net>

但是没有用。

能否请您帮我解决此问题? ABP是否支持将日志写入数据库?

0 个答案:

没有答案