1. 程式人生 > 實用技巧 >NLog 日誌無法寫Pgsql資料庫

NLog 日誌無法寫Pgsql資料庫

檢查步驟

1.欄位是否長度足夠

2.欄位型別是否有非字串型別

如果有需要指定DbType

<parameter name="@createtime" layout="${date}" dbType="DbType.DateTime" />

3.dbProvider="Npgsql.NpgsqlConnection, Npgsql"

4.<connectionString>${var:connectionString}</connectionString>

貼一個可用的配置

<?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"
  throwExceptions="false" >

  <!--日誌級別:Trace,Debug,Info,Warn,Error,Fatal-->

  <!--當你不僅僅只使用 NLog 這一個基礎的 dll ,並使用了一些基於 NLog 擴充套件的工具時,
  你就需要在 extensions 節點下面新增引用的程式集名稱-->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="NLog.Mongo"/>
  </extensions>

  <targets>
    <!--文字-->
    <target name="filelog1" xsi:type="File"
        layout="${time} ${threadid}|${level}| ${logger} ${message} ${exception:format=ToString}"
        fileName="${basedir}/logs/${shortdate}.log" />

    <!--黑洞-->
    <target xsi:type="Null" name="blackhole" />

    <!-- write log to mongodb-->
    <target xsi:type="Mongo"
            name="mongo"
            databaseName="Logs"
            collectionName="Logs"
            connectionString="mongodb://root:[email protected]:27017/Logs"
            cappedCollectionSize="26214400">
      <!--<property name="LongDate" layout="${longdate}" bsonType="DateTime" />
      <property name="Level" layout="${level}" />
      <property name="Logger" layout="${logger}"/>
      <property name="Message" layout="${message}" />
      <property name="Exception" layout="${exception:format=tostring}" />
      <property name="Url" layout="${aspnet-request-url}" />
      <property name="Action" layout="${aspnet-mvc-action}" />
      <property name="UserName" layout="${windows-identity}" />-->
    </target>

    <!--寫入資料庫-->
    <!--dbProvider="MySql.Data.MySqlClient.MySqlConnection, Mysql.Data"-->
    <!--dbProvider="Npgsql.NpgsqlConnection, Npgsql"-->
    <!--<connectionString>${var:connectionString}</connectionString>-->
    <!--<connectionString>Server=192.168.0.130;Database=financial;Uid=root;Pwd=pwd123;Pooling=true;</connectionString>-->
    <target xsi:type="Database" name="pgsqldb"
            dbProvider="Npgsql.NpgsqlConnection, Npgsql"
            >
      <connectionString>${var:connectionString}</connectionString>
      <commandText>
        insert into NLogInfo (
        CreateTime,
        Level, Message,
        Url,CallSite2,
        Exception,
        IP,Users,UserName,Browser
        ) values (
        @createtime,
        @level, @message,
        @url,@callSite2,
        @exception,
        @IP,@Users,@UserName,@Browser
        );
      </commandText>

      <!--<parameter name="@guid" layout="${guid}" />-->
      <parameter name="@createtime" layout="${date}" dbType="DbType.DateTime" />
      <!--<parameter name="@threadid" layout="${threadid}" />-->

      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="${message}" />

      <parameter name="@url" layout="${aspnet-Request-Url}" />
      <parameter name="@callSite2" layout="${callsite}" />

      <parameter name="@exception" layout="${exception:format=ToString}" />

      <parameter name="@IP" layout="${aspnet-request-ip}" />
      <parameter name="@Users" layout="${aspnet-user-identity}" />
      <parameter name="@UserName" layout="${event-context:item=UserName}" />
      <parameter name="@Browser" layout="${aspnet-request-useragent}" />
      <!--<parameter name="@activityid" layout="${activityid}" dbType="DbType.Guid" allowDbNull="true" />-->

<![CDATA[
CREATE TABLE NLogInfo (
ID bigserial,
CreateTime timestamp without time zone,
ThreadID integer,

Level character varying(50),
Message text,

Url text,
CallSite2 text,

Exception text,

IP character varying(200),
Users character varying(200),
UserName character varying(200),
Browser character varying(500),
PRIMARY KEY(ID)
);
    ]]>
    </target>


  </targets>

  <!--控制NLog日誌輸出級別-->
  <!--路由表,日誌是從上到下匹配的-->
  <!--final用法,標識哪個是最終規則,在最終規則匹配之前不處理任何規則-->
  <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.*" minlevel="Trace" maxLevel="Info" writeTo="blackhole" final="true" />

    <!--Add logs to file-->
    <!--<logger name="*" minlevel="Info" writeTo="filelog1" />-->

    <!--Add logs to mongodb-->
    <!--<logger name="*" minlevel="Trace" writeTo="mongo"/>-->
    <!--新增到資料庫-->
    <logger name="*" minlevel="Trace" writeTo="pgsqldb" />

  </rules>
</nlog>

  

DbProvider Examples

MySql and .NET Core

Install package:https://www.nuget.org/packages/MySql.Data/

dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"

or if using packages:https://www.nuget.org/packages/MySqlConnector/and MySqlConnector.Logging.NLog

dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySqlConnector"

System.Data.SQLite and .NET Core

Install package:https://www.nuget.org/packages/System.Data.SQLite

dbProvider="System.Data.SQLite.SQLiteConnection, System.Data.SQLite"

Microsoft.Data.SqlClient and .NET Core

Install package:https://www.nuget.org/packages/Microsoft.Data.SqlClient/

dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient"

Microsoft.Data.Sqlite and .NET Core

Install package:https://www.nuget.org/packages/Microsoft.Data.SQLite/

dbProvider="Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite"

Npgsql and .NET core

Install package:https://www.nuget.org/packages/Npgsql/

dbProvider="Npgsql.NpgsqlConnection, Npgsql"

Oracle.ManagedDataAccess and .NET

dbProvider="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess"

Mono.Data.Sqlite and .NET

dbProvider="Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite"