NLog不写入数据库表

时间:2013-12-13 16:07:27

标签: nlog

这是我的NLog配置文件。

<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true"  internalLogLevel="Debug" internalLogToConsole="true"
     internalLogFile="c:\\temp\\nlog.txt" 
      >

  <targets>
    <!--Useful for debugging-->
    <target name="consolelog" type="ColoredConsole"
    layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />

    <target name="filelog" type="File" fileName="c:\\temp\\nlog1.txt"
    layout="${date}: ${message}" />

    <target name="eventlog" type="EventLog" source="My App" log="Application"
    layout="${date}: ${message} ${stacktrace}" />

    <target name="databaselog" type="Database">

      <dbProvider>sqlserver</dbProvider>

      <!-- database connection parameters -->
      <!-- alternatively you could provide a single 'connectionstring' parameter -->
      <connectionString>Data Source=.\SQLEXPRESS;Initial Catalog=SnSolutions;User Id=sa;Password=test@1234@;</connectionString>
      <!--<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyTest;User Id=mytest;Password=mytest123;" providerName="System.Data.SqlClient" />-->

      <commandText>
        insert into NLog_Error ([time_stamp],[level],[host],[type],1,[logger],[message],[stacktrace],[allxml]) values(@time_stamp,@level,@host,@type,@source,@logger,@message,@stacktrace,@allxml);
      </commandText>

      <parameter name="@time_stamp" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@host" layout="${machinename}" />
      <parameter name="@type" layout="${exception:format=type}" />
      <parameter name="@source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@stacktrace" layout="${exception:stacktrace}" />
      <parameter name="@allxml" layout="${web_variables}" />

    </target>

  </targets>

  <rules>
    <!--
 <logger name="*" minlevel="Fatal" writeTo="eventlog" />
 -->
    <logger name="*" minlevel="Info" writeTo="filelog" />
    <logger name="*" minlevel="Info" writeTo="databaselog" />
  </rules>

</nlog>

这是我的NLogLogger类

public class NLogLogger 
{
    private readonly Logger _logger;

    public NLogLogger(Logger logger)
    {

        _logger = logger;
    }

    public NLogLogger()
    {
        StackFrame frame = new StackFrame(1, false);

        _logger = LogManager.GetLogger(frame.GetMethod().DeclaringType.FullName);

    }

    public void Trace(string message)
    {
        _logger.Trace(message);
    }

    public void Debug(string message)
    {
        _logger.Debug(message);
    }

    public void Info(string message)
    {
        _logger.Info(message);
    }

    public void Warn(string message)
    {
        _logger.Warn(message);
    }

    public void Error(string message)
    {
        _logger.Error(message);
    }

    public void Fatal(string message)
    {
        _logger.Fatal(message);
    }
}

我试图以下列方式使用。

NLogLogger logger = new NLogLogger();

logger.Info("We're on the Index page for Activities");

但是无法在数据库中看到任何记录,也无法在文件系统中看到任何错误。

请告诉我哪个是我缺少的部分。

提前致谢。

1 个答案:

答案 0 :(得分:7)

插入命令有错误 - 1不是有效的列名。

insert into NLog_Error (...[type],1,[logger]..) 

以下是关于记录到数据库的example