Logging a C # application to a FireBird Embedded database using NLog 2.0
Getting started with my first desktop application in C #, I wondered about logging. Having studied the proposals on this topic, for good reviews and the lack of fees for use, my choice fell on NLog 2.0. After reading the documentation on the site, as well as local articles, I easily configured the output of debugging information to a text file. But the inquiring mind does not stand still, and since my application uses the FireBird Embedded database, I decided to configure logging in it. This is where I got a 5-hour puzzle.There is practically no information on setting up NLog for FireBird (whether Embedded or not plays a big role), and the one that exists is related to earlier versions of NLog and has already lost relevance. Rolling up my sleeves and stocking up with kefir, I began to overcome this obstacle, which is what this article is about.
I want to note that the article does not affect the connection of the FireBird .Net provider to the project, as well as the initial installation of NLog.
Configuring NLog is to properly configure the NLog.config file, which is an XML file. In the first couple, I ran into a problem that after turning on the logging system, the project simply stopped starting, failing to initialize the main form of the application. The reason for this was my poorly configured NLog. To simplify your task and save your nerves, you need to add two lines to the configuration file: The next time you start the application, file.txt will be created in the folder with the executable file, which contains a fairly detailed log of the NLog system itself and will allow you to catch configuration errors. The configuration of the "receiver" of logs is in the tag "target". The most difficult thing was to get 3 things: 1) The name of the DBMS provider with its token 2) The connection string to the database
3) The query to the database and its parameters
The first item was overcome with the help of Google. But with the second and third paragraph it was more difficult. I got the connection string from my application, which, based on other manuals, could already connect to the FireBird database. I sorted out the request and its parameters using the not-so-scientific “poke” method. In order not to languish, I’ll immediately give a working “receiver” config: dbProvider is the name of the FireBird .NET provider, which corresponds to its current official version 2.6.5. For future versions, you only need to change the digits, the Token (PublicKeyToken = 3750abcc3150b00c) should remain the same. connectionString
- connection string. By and large, when it is formed, it will not be difficult to understand and change something in it for yourself. In this example, the connection is configured on the FireBird Embedded server, the FYT.FDB database, which lies in the directory with the executable file. For FireBird Classic or SuperServer you only need to change “server type” and “client library”.
commandText - a request to insert data into the database. The parameters that are transmitted by the logger are marked by a "dog", i.e. @. Below, the attributes in the “parameter” tags show NLog what each parameter in the request needs to be replaced. A list of all available layout options can be found on the NLog website.
In conclusion, I will give a complete listing of the configuration file NLog.config:
Thanks for attention! I hope my article helps you not to spend 5 hours setting up a logging system in the FireBird database!
I want to note that the article does not affect the connection of the FireBird .Net provider to the project, as well as the initial installation of NLog.
Configuring NLog is to properly configure the NLog.config file, which is an XML file. In the first couple, I ran into a problem that after turning on the logging system, the project simply stopped starting, failing to initialize the main form of the application. The reason for this was my poorly configured NLog. To simplify your task and save your nerves, you need to add two lines to the configuration file: The next time you start the application, file.txt will be created in the folder with the executable file, which contains a fairly detailed log of the NLog system itself and will allow you to catch configuration errors. The configuration of the "receiver" of logs is in the tag "target". The most difficult thing was to get 3 things: 1) The name of the DBMS provider with its token 2) The connection string to the database
throwExceptions="true"
internalLogFile="file.txt"
3) The query to the database and its parameters
The first item was overcome with the help of Google. But with the second and third paragraph it was more difficult. I got the connection string from my application, which, based on other manuals, could already connect to the FireBird database. I sorted out the request and its parameters using the not-so-scientific “poke” method. In order not to languish, I’ll immediately give a working “receiver” config: dbProvider is the name of the FireBird .NET provider, which corresponds to its current official version 2.6.5. For future versions, you only need to change the digits, the Token (PublicKeyToken = 3750abcc3150b00c) should remain the same. connectionString
<target
xsi:type="Database"
name="db"
dbProvider="FirebirdSql.Data.FirebirdClient.FbConnection, FirebirdSql.Data.FirebirdClient, Version=2.6.5.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c"
useTransactions="true"
connectionString="server type=Embedded;initial catalog=FYT.FDB;character set=WIN1251;dialect=3;client library=fbembed.dll;user id=SYSDBA;password=masterkey"
keepConnection="true"
commandText="INSERT INTO logs (DT, LOG_TEXT, LEVEL_ID, CLASS, STACK_TRACE) values (@DT, @LOG_TEXT, @LEVEL_ID, @CLASS, @STACK_TRACE);">
<parameter layout="${longdate}" name="@DT" />
<parameter layout="${level}" name="@LEVEL" />
<parameter layout="${message}" name="@LOG_TEXT" />
<parameter layout="${logger}" name="@CLASS" />
<parameter layout="${stacktrace}" name="@STACK_TRACE" />
</target>
* This source code was highlighted with Source Code Highlighter.
- connection string. By and large, when it is formed, it will not be difficult to understand and change something in it for yourself. In this example, the connection is configured on the FireBird Embedded server, the FYT.FDB database, which lies in the directory with the executable file. For FireBird Classic or SuperServer you only need to change “server type” and “client library”.
commandText - a request to insert data into the database. The parameters that are transmitted by the logger are marked by a "dog", i.e. @. Below, the attributes in the “parameter” tags show NLog what each parameter in the request needs to be replaced. A list of all available layout options can be found on the NLog website.
In conclusion, I will give a complete listing of the configuration file NLog.config:
<?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"
throwExceptions="true"
internalLogFile="file.txt">
<targets>
<target
xsi:type="Database"
name="db"
dbProvider="FirebirdSql.Data.FirebirdClient.FbConnection, FirebirdSql.Data.FirebirdClient, Version=2.6.5.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c"
useTransactions="true"
connectionString="server type=Embedded;initial catalog=FYT.FDB;character set=WIN1251;dialect=3;client library=fbembed.dll;user id=SYSDBA;password=masterkey"
keepConnection="true"
commandText="INSERT INTO logs (DT, LOG_TEXT, LEVEL_ID, CLASS, STACK_TRACE) values (@DT, @LOG_TEXT, @LEVEL_ID, @CLASS, @STACK_TRACE);">
<parameter layout="${longdate}" name="@DT" />
<parameter layout="${level}" name="@LEVEL" />
<parameter layout="${message}" name="@LOG_TEXT" />
<parameter layout="${logger}" name="@CLASS" />
<parameter layout="${stacktrace}" name="@STACK_TRACE" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="db" final="true"/>
</rules>
</nlog>
* This source code was highlighted with Source Code Highlighter.
Thanks for attention! I hope my article helps you not to spend 5 hours setting up a logging system in the FireBird database!