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 =]
To start logging, we need to create a Logger instance. Before creating the Logger instance, we are configuring Nlog by passing the configuration file nlog. config which we are going to create in the next section. The GetCurrentClassLogger() method returns the Logger instance with the name of the current class (Nlog.
The following types can be configured: Targets - the destinations of a logevent, e.g. file, database, console. Layout - the layout e.g. json, csv, plain-text (default) Layout renderers - the template markers, e.g. ${message}, ${exception}, ${date}
NLog is a flexible and free logging platform for various . NET platforms, including . NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With