I'm working on an application, and have some registry screens, and I want to add a log routine to the API of these records, type: do not add, update, and error occurrences (if any).
The database that is being used is PostgreSQL and here comes the problem, I already researched several blogs on how to use Log4net or NLog and save the logs in the PostgreSQL database, but I did not find it, I already made the lib two frameworks for SqlServer and it works, but when I switch to the PostgreSQL configuration it does not work.
Has anyone set up Log4Net or NLog to write logs to the PostGre database?
Here are the files of how I am doing:
NLog.config
<targets>
<target name="database" xsi:type="Database">
<dbProvider>
Npgsql.NpgsqlConnection,Npgsql,Version=1.22.2,Culture=neutral,PublicKeyToken=5d8b90d52f46fda7
</dbProvider>
<connectionString>
Server=SERVER;Port=0000;User Id=user;Password=pass;Database=bd;
</connectionString>
<commandText>
insert into logs_nlog(level, callsite, type, message, stacktrace, innerexception, additionalinfo, loggedondate) values (@level, @callSite, @type, @message, @stackTrace, @innerException, @additionalInfo);
</commandText>
<parameter name="@level" layout="${level}" />
<parameter name="@callSite" layout="Chamada: ${callsite} Linha: ${callsite-linenumber}" />
<parameter name="@type" layout="${exception:format=type}" />
<parameter name="@message" layout="${exception:format=message}" />
<parameter name="@stackTrace" layout="${exception:format=stackTrace}" />
<parameter name="@innerException" layout="${exception:format=:innerFormat=ShortType,Message,Method:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}" />
<parameter name="@additionalInfo" layout="${message}" />
</target>
In the Client class
static void Main(string[] args)
{
Log Log = new Log();
try
{
Log.Info("Starting");
int zero = 0;
Log.Debug("Trying");
int result = 5 / 0;
}
catch (DivideByZeroException ex)
{
Log.Error("OMG!", ex);
}
Console.WriteLine("something wrong");
Console.ReadKey();
}