如何讓NLog寫入數據庫

[英]How to get NLog to write to database


I'm trying to get NLog to log to my database log table but to no avail. I'm sure my connection string is correct because it's the same used elsewhere in my web.config. Writing out to a file works fine, so I know it's not just NLog, but must be something I'm doing wrong. Below is my NLog configuration:

我正在嘗試讓NLog登錄到我的數據庫日志表但無濟於事。我確定我的連接字符串是正確的,因為它與我的web.config中的其他地方一樣。寫出文件工作正常,所以我知道它不僅僅是NLog,而且必須是我做錯了。以下是我的NLog配置:

<!-- NLOG CONFIGURATION -->
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="file" xsi:type="File" fileName="${basedir}/logs/Log ${shortdate}.txt" layout="${longdate} ${callsite} ${level}: ${message} ${exception:format=Message,StackTrace} ${stacktrace}" />
      <target type="Database" name="database" connectionstring="MyConnectionString">
        <commandText>
          insert into MyLog ([CreateDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace);
        </commandText>
        <parameter name="@createDate" layout="${longdate}"/>
        <parameter name="@origin" layout="${callsite}"/>
        <parameter name="@logLevel" layout="${level}"/>
        <parameter name="@message" layout="${message}"/>
        <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/>
        <parameter name="@stackTrace" layout="${stacktrace}"/>
      </target>
    </targets>
    <rules>
      <logger name="*" writeTo="file"/>
      <logger name="*" appendTo="database"/>
      <!--<logger name="*" writeTo="mail" minlevel="Error"/>-->
    </rules>
  </nlog>

2 个解决方案

#1


Try putting the following in your nlog tag:

嘗試將以下內容放入nlog標記中:

<nlog throwExceptions="true" internalLogFile="c:\nlog.txt" internalLogLevel="Debug" />

That might help determine what the problem is

這可能有助於確定問題所在

#2


NLog allows for logging the internals of the framework itself.

NLog允許記錄框架本身的內部。

Enable "debug level for your internal logging" for NLog and see what's going wrong.

為NLog啟用“內部日志記錄的調試級別”,看看出了什么問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/17/72f4118ef35457a96527300c853b7111.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com