C# 如何让 NLog 写入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1008093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:24:08  来源:igfitidea点击:

How to get NLog to write to database

c#web-confignlog

提问by Ciaran O'Neill

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>

采纳答案by slolife

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

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

回答by Sunny Milenov

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启用“内部日志的调试级别”,看看出了什么问题。