简体   繁体   English

如何配置NLog写入数据库?

[英]How do I configure NLog to write to a database?

I'm trying to get NLog to write to a database, however with my current code it throws an exception when I attempt to debug, the exception is: The type initializer for 'NotifyIcon.Program' threw an exception. 我正在尝试让NLog写入数据库,但是当我尝试调试时,我的当前代码会引发异常,例外是:'NotifyIcon.Program'的类型初始化程序引发了异常。

my NLog configuration file code is below, as this seems to be causing the issue as it's the only code I've changed. 我的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" autoReload="true">

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

    <target name="database" xsi:type="Database" />
    <target xsi:type="Database"
          name="String"
          dbUserName="Layout"
          dbProvider="sqlserver"
          useTransactions="false"
          connectionStringName="String"
          connectionString="Data Source=AC-02\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
          keepConnection="true"
          dbDatabase="Layout"
          dbPassword="Layout"
          dbHost="Layout"
          installConnectionString="Layout"
          commandText="INSERT INTO Logs (Machine_Name, Username, Logon_Time, Screensaver_On, Screensaver_Off, Logoff_Time, Program_Start) Values @MachineName, @Username, @LogonTime, @Screensaver_On, @Screensaver_Off, @LogoffTime, @ProgramStart "/>

  </targets>

  <rules>

    <logger name="*" minlevel="Trace" writeTo="database" />

  </rules>
</nlog>

any and all help would be greatly appreciated =] 任何和所有的帮助将不胜感激=]

You seem to be missing the parameters that are to be inserted. 您似乎缺少要插入的参数。

See the examples at http://justinpdavis.blogspot.com/2010/04/logging-to-database-with-nlog.html 请参阅http://justinpdavis.blogspot.com/2010/04/logging-to-database-with-nlog.html上的示例

The nLog web page doesn't make it very clear that these are required, but if you squint your eyes and read https://github.com/nlog/NLog/wiki/Database-target you should find that they are required. nLog网页并不十分清楚这些是必需的,但是如果你眯着眼睛看看https://github.com/nlog/NLog/wiki/Database-target,你会发现它们是必需的。

U also wrote 2 targets. 你还写了2个目标。 And also a lot of attributes that u don't need to set. 还有很多你不需要设置的属性。 Should just be: 应该只是:

<target name="DbLog" xsi:type="Database" connectionString="YourConStr" 
        commandText="insert into [blablablabal] (Col1) values (@val1)">
  <parameter name="@val1" layout="${level}" /></target>

Something like this. 像这样的东西。 Easy no? 容易没? :) :)

It looks your insert string is not in the right format? 它看起来你的插入字符串格式不正确? You are missing () around the parameters list. 参数列表周围缺少()。

commandText="INSERT INTO Logs (Machine_Name, Username, Logon_Time, Screensaver_On, Screensaver_Off, Logoff_Time, Program_Start) Values (@MachineName, @Username, @LogonTime, @Screensaver_On, @Screensaver_Off, @LogoffTime, @ProgramStart) "

A simple example, 一个简单的例子,

Config: 配置:

<target type="Database" name="database" connectionstring="Server=localhost;Database=NLog;Trusted_Connection=True;">
  <commandText>
    INSERT INTO NLogEntries ([Origin], [Message], [LogLevel],[CreatedOn],[OrderId]) VALUES (@Origin,@Message,@LogLevel,@Date, @OrderId);
  </commandText>
  <parameter name="@Date" layout="${date}" dbType="DbType.Date"/>
  <parameter name="@Origin" layout="${callsite}"/>
  <parameter name="@LogLevel" layout="${level}"/>
  <parameter name="@message" layout="${message}"/>
  <parameter name="@OrderId" layout="${event-properties:MyOrderId}" dbType="DbType.Int32"/> <!-- custom field! Note also the DB Type. Using Logger.WithProperty -->
</target>

Note, NLog 4.6 has also support for DbType - See https://nlog-project.org/2019/03/20/nlog-4-6-is-live.html 注意,NLog 4.6也支持DbType - 请参阅https://nlog-project.org/2019/03/20/nlog-4-6-is-live.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NLog - 如何配置有条件地写入数据库以及电子邮件异常和文件异常 - NLog - how to configure to conditionally write to Database and on exception to Email and on exception to File 如何配置 NLog 以写入 Azure 应用服务的日志 stream? - How do I configure NLog to write to Azure App Service's log stream? 如何让NLog写入数据库 - How to get NLog to write to database 如何将nlog中的值放入数据库中? - How do I put values from nlog into a database? 如何将 NLog 中的自定义字段记录到数据库? - How do I log a custom field in NLog to database? 如何在 .NET Core 3 web 应用程序的代码中配置 NLog 以匹配“Microsoft.*”记录器名称且无目标? - How do I configure NLog in code in a .NET Core 3 web app to match the “Microsoft.*” logger name and no target? 如何配置ReflectInsight日志查看器以从数据库读取nlog数据? - How to configure ReflectInsight log viewer to read nlog data from database? 不要将日志写入数据库NLog + ASP .NET 5 + SQL Server - Do not write logs to the database NLog + ASP .NET 5 + SQL Server 如何使用 NLog 配置 API 将 NULL 写入数据库中的列? - How to write NULL to a column in a database using NLog Configuration API? 我是否需要为NLog编写任何结束代码? - Do I need to write any closing code for NLog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM