【问题标题】:How to get NLog to write to database如何让 NLog 写入数据库
【发布时间】:2009-06-17 16:12:09
【问题描述】:

我试图让 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>

【问题讨论】:

    标签: c# web-config nlog


    【解决方案1】:

    尝试将以下内容放入您的 nlog 标记中:

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

    这可能有助于确定问题所在

    【讨论】:

    • ${longdate} 是我试图插入日期时间字段的字符串。将其更改为 ${date} 就可以了。
    • 请注意,NLog 4.6+ 支持数据库目标中的 Db 类型。所以你可以设置为 ${date} ,dbType=DbType.Date,见nlog-project.org/2019/03/20/nlog-4-6-is-live.html
    【解决方案2】:

    NLog 允许记录框架本身的内部。

    为 NLog 启用“debug level for your internal logging”,看看出了什么问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多