Aspetcore: NLog with Postgresql

This Article is base ASP.Net Core 2 and the latest version of Nlog.

1. Install NLog and the Postgresql Package
Using NuGet to install the package into your ASP.NET Core project:
PM > Install-Package NLog.Web.AspNetCorePM > Install-Package Npgsql
3. Register NLog
Add in your Startup.cs 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

    // make sure Chinese chars don't fk up

    //add NLog to ASP.NET Core

    //add NLog.Web
4. Add to Your Controller
Add a logger object to your Controller.
private readonly ILogger<YourController> _logger;
Modify the constructor to use it
public YourController(ILogger<YourController> logger = null)
    if (null != logger)
        _logger = logger;
5. Use Logging APIs
Now, you can use the logging APIs like this:
_logger.LogInformation($"Created OrderId: {order.OrderId}, FriendlyId: {order.FriendlyId}, User: {user.UserName}.");
6. Using it with Postgresql 
<?xml version="1.0" encoding="utf-8" ?>
    <target xsi:type="File" name="allfile" fileName="${gdc:item=configDir}\nlog-all.log"
                layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
    <target xsi:type="File" name="ownFile-web" fileName="${gdc:item=configDir}\nlog-own.log"
             layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />
    <target xsi:type="Null" name="blackhole" />
    <target name="database" xsi:type="Database"
              dbProvider="Npgsql.NpgsqlConnection, Npgsql"
              connectionString="User ID=damienbod;Password=damienbod;Host=localhost;Port=5432;Database=log;Pooling=true;"
    Id serial primary key,
    Application character varying(100) NULL,
    Logged text,
    Level character varying(100) NULL,
    Message character varying(8000) NULL,
    Logger character varying(8000) NULL,
    Callsite character varying(8000) NULL,
    Exception character varying(8000) NULL
              insert into logs (
              Application, Logged, Level, Message,
              Logger, CallSite, Exception
              ) values (
              @Application, @Logged, @Level, @Message,
              @Logger, @Callsite, @Exception
          <parameter name="@application" layout="AspNetCoreNlog" />
          <parameter name="@logged" layout="${date}" />
          <parameter name="@level" layout="${level}" />
          <parameter name="@message" layout="${message}" />
          <parameter name="@logger" layout="${logger}" />
          <parameter name="@callSite" layout="${callsite:filename=true}" />
          <parameter name="@exception" layout="${exception:tostring}" />
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <logger name="*" minlevel="Trace" writeTo="database" />
    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />

Popular posts from this blog

UI-Bootstrap Collapsible Sticky Footer

Installing Windows on Acer chromebook 15 cb3-532