NLog save entries to database(postgresql),

509 views Asked by At

Search on many post, all compared to almost identical but somehow its doesn't send entries to DB.

logging file works, but database not..

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"
      autoReload="true"
      internalLogLevel="Trace"
      internalLogFile="c:logs\internallog.txt">
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <targets>
    <target name="database" xsi:type="Database"
            dbProvider="Npgsql.NpgsqlConnection, Npgsql"
            connectionString="User ID=postgres;Password=123456;Host=localhost;Port=5432;Database=Telepati_Dev;">
      <!--//Pooling=true;-->
      <commandText>
        insert into systemlogs(log_date,log_level,log_logger,log_message,log_machine_name, log_user_name, log_call_site, log_thread, log_exception, log_stacktrace) values(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
      </commandText>
      <parameter name="@time_stamp" layout="${longdate}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@machinename" layout="${machinename}" />
      <parameter name="@user_name" layout="${windows-identity:domain=true}" />
      <parameter name="@call_site" layout="${callsite:filename=true}" />
      <parameter name="@threadid" layout="${threadid}" />
      <parameter name="@log_exception" layout="${exception}" />
      <parameter name="@stacktrace" layout="${stacktrace}" />
    </target>
    <target xsi:type="File" name="ownFile-web" fileName="c:\logs\ApplicationLogs-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} CorrelationId=${aspnet-item:variable=CorrelationId} ${exception:format=tostring}|url: ${aspnet-request-url}|controller: ${aspnet-MVC-Controller}, action: ${aspnet-mvc-action}" />
  </targets>
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

Api project gets dbcontext from data layer so does Npgsql reference

here is API nugget refs;

enter image description here

and data layer packages;

enter image description here

here are some contents of "internallog.txt";

2020-05-31 02:07:32.3212 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.3503 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.3642 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.3774 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.3934 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.4091 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.4245 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.4403 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.4551 Trace Opening c:\logs\ApplicationLogs-2020-05-31.log with allowFileSharedWriting=False
2020-05-31 02:07:32.4771 Debug Targets for Microsoft.EntityFrameworkCore.Database.Command by level:
2020-05-31 02:07:32.4771 Debug Trace =>
2020-05-31 02:07:32.4771 Debug Debug =>
2020-05-31 02:07:32.4947 Debug Info =>
2020-05-31 02:07:32.4947 Debug Warn => ownFile-web
2020-05-31 02:07:32.4947 Debug Error => ownFile-web
2020-05-31 02:07:32.5119 Debug Fatal => ownFile-web
2020-05-31 02:07:32.5119 Debug Targets for Microsoft.EntityFrameworkCore.Query by level:
2020-05-31 02:07:32.5119 Debug Trace =>
2020-05-31 02:07:32.5279 Debug Debug =>
2020-05-31 02:07:32.5279 Debug Info =>
2020-05-31 02:07:32.5279 Debug Warn => ownFile-web
2020-05-31 02:07:32.5428 Debug Error => ownFile-web
2020-05-31 02:07:32.5428 Debug Fatal => ownFile-web
2020-05-31 02:07:32.5428 Debug Targets for Microsoft.EntityFrameworkCore.Database.Transaction by level:
2020-05-31 02:07:32.5594 Debug Trace =>
2020-05-31 02:07:32.5594 Debug Debug =>
2020-05-31 02:07:32.5704 Debug Info =>
2020-05-31 02:07:32.5704 Debug Warn => ownFile-web
2020-05-31 02:07:32.5704 Debug Error => ownFile-web
2020-05-31 02:07:32.5856 Debug Fatal => ownFile-web
2020-05-31 02:07:32.5856 Debug Targets for Microsoft.EntityFrameworkCore.Database.Connection by level:
2020-05-31 02:07:32.5856 Debug Trace =>
2020-05-31 02:07:32.6033 Debug Debug =>
2020-05-31 02:07:32.6033 Debug Info =>
2020-05-31 02:07:32.6033 Debug Warn => ownFile-web
2020-05-31 02:07:32.6186 Debug Error => ownFile-web
2020-05-31 02:07:32.6186 Debug Fatal => ownFile-web
2020-05-31 02:07:32.6403 Debug Targets for Microsoft.EntityFrameworkCore.Update by level:

whats missing here?

1

There are 1 answers

0
Julian On BEST ANSWER

There isn't a rule that writes to the database target in your config.

E.g. Replace

<logger name="*" minlevel="Trace" writeTo="ownFile-web" />

With

<logger name="*" minlevel="Trace" writeTo="ownFile-web, database" />