📜 ⬆️ ⬇️

Logging a C # application to a FireBird Embedded database using NLog 2.0

Getting started on 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 set up debug information output to a text file. But an inquiring mind does not stand still, and since my application uses the FireBird Embedded database, I decided to set up logging in it. That's where I got a puzzle of 5 hours. There is practically no information on configuring NLog for FireBird (whether embedded or not plays a large role), and the one that exists is relevant to earlier versions of NLog and has already lost relevance. Having rolled 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.

NLog configuration is the correct configuration of the NLog.config file, which is an XML file. In the first couple, I ran into the problem that after switching on the logging system, the project simply stopped running, failing to initialize the main form of the application. This was due to a poorly configured NLog. To simplify your task and save your nerves, you need to add two lines to the configuration file:
')
throwExceptions="true"
internalLogFile="file.txt"

The next time you start the application, a file.txt will be created in the folder with the executable file, which contains a rather detailed log of the NLog system itself and will allow you to catch configuration errors.
Setting the "receiver" logs is in the tag "target". The most difficult was to get 3 things:
1) The name of the database provider with its token
2) DB connection string
3) Query to the database and its parameters

The first point was overcome with the help of Google. But with the second and third paragraph was more difficult. I received the connection string from my application, which, based on other manuals, could already connect to the FireBird database. I disassembled the request and its parameters with the help of a not quite scientific method of “spear”. In order not to torment, I will immediately give the working config of the "receiver":
< 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 .
< 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 .
< 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 .


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 is a 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 the “server type” and “client library”.

commandText - request to insert data into the database. Parameters that are transmitted by the logger are marked with a “dog”, i.e. @. Below, the attributes in the “parameter” tags show the NLog what you need to substitute for each parameter in the request. A list of all available replacement options (layout) 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 .
<? 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 .
<? 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 will help you not to spend 5 hours on setting up the logging system in the FireBird database!

Source: https://habr.com/ru/post/133763/


All Articles