在SQL数据库中插入Log4Net无法正常工作

时间:2012-03-31 15:58:41

标签: c# sql asp.net-mvc-3 log4net

我遇到了Log4Net的问题。它不会在表格中添加任何条目...... 这是表查询

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

)ON [PRIMARY]

GO

并且web.config中的代码如下:

<log4net>
<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=.\SQLEXPRESS;Initial Catalog=pnawebloket;Integrated Security=True" />
  <commandText value="INSERT INTO Log4Net_Error2 ([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>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
  <level value="DEBUG" />
  <appender-ref ref="AdoNetAppender" />
</root>

另外,在Aplication_Start的global.asax.cs中我添加了一行

log4net.Config.XmlConfigurator.Configure();  

我仍然没有参加表格。

public ActionResult Employees()
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(this.GetType());
        logger.Info("test");

        //throw new Exception("ex");
        ViewBag.Message = "Bellow is a table with employee's";
        var employees = employeerepository.FindAllEmployee().ToList();

        return View(employees);
    }

如果我尝试在文本文件中添加消息,那么工作正常。

App在MVC3中,我正在使用sql server 2008r2

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您的日志表:Log4Net_Error;配置文件中的日志表:Log4Net_Error2。您应该在配置文件中将表名更改为Log4Net_Error。

答案 1 :(得分:0)