zoukankan      html  css  js  c++  java
  • 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:pwd123@192.168.0.130: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"
  • 相关阅读:
    iOS 音频视频图像合成那点事
    Android低功耗蓝牙(BLE)使用详解
    android实现超酷的腾讯视频首页和垂直水平网格瀑布流一揽子效果
    基于vue单页应用的例子
    Android 6.0 超级简单的权限申请 (Permission)
    ubuntu创建Centos7镜像&&配置运行环境
    Scikit-learn方法使用总结
    解决TensorFlow最新代码编译错误问题
    jQuery 基础
    数据交换格式 —— JSON(JavaScript Object Notation)
  • 原文地址:https://www.cnblogs.com/huawublog/p/14031746.html
Copyright © 2011-2022 走看看