NLog不会将日志条目写入数据库

时间:2015-07-08 18:20:16

标签: .net nlog

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->

    <target xsi:type="File"
            name="f"
            fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} | ${logger} | ${uppercase:${level}} | ${message}${newline}    ${callsite} | ${stacktrace}" />

    <!-- This will set the normal console. I find the colored much nicer :) -->
    <!--
   <target  xsi:type="Console"
            name="console" 
            layout="${shortdate} ${message}" /> -->

    <target  xsi:type="ColoredConsole"
            name="console"
            layout="${shortdate} ${message}" />

    <!-- my not-working database target -->
    <target xsi:type="Database"
            name="database"
            connectionStringName="CRMTool"
            dbProvider="sqlclient"
            installConnectionString="Data Source=(local);Initial Catalog=CRMTool;Integrated Security=true;"
            commandText="insert into LogEntries(Date,Level,Logger,Message,MachineName, UserName, Callsite, ThreadId, Exception, InnerException, Stacktrace, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) values(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @log_innerexception, @stacktrace, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage);">

      <install-command text="CREATE TABLE [dbo].[LogEntries](
                  [Id] [int] IDENTITY(1,1) NOT NULL,
                  [Level] [varchar](5) NOT NULL,
                  [Message] [varchar](4095) NOT NULL,
                  [Date] [datetime] NOT NULL,
                  [Logger] [varchar](200) NULL,
                  [MachineName] [varchar](100) NULL,
                  [UserName] [varchar](100) NULL,
                  [CallSite] [varchar](100) NULL,
                  [ThreadId] [varchar](100) NULL,
                  [Exception] [varchar](max) NULL,
                  [InnerException] [varchar](max) NULL,
                  [Stacktrace] [varchar](max) NULL,
                  ErrorSource [varchar](max) NULL,
                  ErrorClass [varchar](max) NULL,
                  ErrorMethod [varchar](max) NULL,
                  ErrorMessage [varchar](max) NULL,
                  InnerErrorMessage [varchar](max) NULL,
                  CONSTRAINT [PK_LogEntries] PRIMARY KEY CLUSTERED
                  ([Id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
                  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]" connectionString="Data Source=(local);Initial Catalog=CRMTool;Integrated Security=true;" ignoreFailures="false" />


      <uninstall-command text="DROP TABLE [dbo].[LogEntries]"
          connectionString="Data Source=(local);Initial Catalog=CRMTool;Integrated Security=true;" ignoreFailures="false" />

      <parameter name="@time_stamp" layout="${longdate}"/>
      <parameter name="@level" layout="${level:uppercase=true}"/>
      <parameter name="@logger" layout="${logger}"/>
      <parameter name="@message" layout="${message}"/>
      <parameter name="@machinename" layout="${machinename}"/>
      <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
      <parameter name="@call_site" layout="${callsite:filename=true}"/>
      <parameter name="@threadid" layout="${threadid}"/>
      <parameter name="@ErrorSource" layout="${event-context:item=error-source}" />
      <parameter name="@ErrorClass" layout="${event-context:item=error-class}" />
      <parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
      <parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
      <parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
      <parameter name="@log_exception" layout="${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}"/>
      <parameter name="@log_innerexception" layout="${exception:method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}"/>
      <parameter name="@stacktrace" layout="${stacktrace}"/>
    </target>

  </targets>

  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Trace" writeTo="f" />
    <logger name="*" minlevel="Trace" writeTo="console" />
    <logger name="*" minlevel="Trace" writeTo="database" />
  </rules>
</nlog>

另一篇帖子Target database not working with NLog 3.1 and Windsor Castle Logging Facility提供了一些提示,质疑是否手动创建了LogEntries表,并声明install-command没有自动运行,因为NLOG本身并不运行它们。名为InstallNLogConfig.exe的命令行工具应该运行命令,这很奇怪&#34;从nuget包中删除。

来自Error in execution while using NLog的解决方案也无效。我想在标签部分指定NLog.2.1.0,但这个信息需要更高的声誉级别。无论如何...

我很感激任何导致让程序将日志条目写入SqlServer数据库的想法。谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于insert语句:尝试插入字符串日期,其中datetime是预期的,[CallSite]大小也不足,所以我将其增加到最大值。一旦这些问题得到解决,它就有用了!