如何配置NLog写入数据库? [英] How do I configure NLog to write to a database?
问题描述
我试图让NLog写入数据库,但是使用我当前的代码,当我尝试调试时它会抛出异常,异常是:'NotifyIcon.Program'的类型初始化程序抛出异常。 p>
我的NLog配置文件代码如下,因为这似乎是导致的问题,因为它是我改变了唯一的代码。
<?xml version =1.0encoding =utf-8?>
< nlog xmlns =http://www.nlog-project.org/schemas/NLog.xsd
xmlns:xsi =http://www.w3.org/2001/XMLSchema -instanceautoReload =true>
<! -
有关自定义日志记录规则和输出的信息,请参阅http://nlog-project.org/wiki/Configuration_file
。
- >
< targets>
<! - 在这里添加您的目标 - >
< target name =databasexsi:type =Database/>
< target xsi:type =Database
name =String
dbUserName =Layout
dbProvider =sqlserver
useTransactions =false
connectionStringName =String
connectionString =Data Source = AC-02\SQLEXPRESS;初始目录= master;集成安全= 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)值@MachineName,@Username,@LogonTime,@Screensaver_On,@Screensaver_Off,@LogoffTime,@ProgramStart/>
< / targets>
< rules>
< logger name =*minlevel =TracewriteTo =database/>
< / rules>
< / nlog>
任何和所有的帮助将非常感谢=]
nLog网页并不能清楚地说明这些是必需的,但如果你眯起眼睛看着https://github.com/nlog/NLog/wiki/Database-target 您应该会发现它们是必需的。 / p>
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.
my NLog configuration file code is below, as this seems to be causing the issue as it's the only code I've changed.
<?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
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写入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!