Using Nlog with Postgresql C#

Asked

Viewed 36 times

2

I am working on an application, and have some log screens, and I want to add a log routine in 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, I already made the lib of the two frameworks for Sqlserver and it works, but when I switch to Postgresql configuration it does not work.

Someone set up Log4net or Nlog to record logs in the Postgre database?

Here are the files on how I’m 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();
}
  • Is there an error message? If yes, you can inform to help you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.