log4net输出到PostgreSql

PgSQL越来越强大了。小项目输出log到sqlite是挺好的,大东西尤其是要长期运行的程序还是输出到PostgreSQL比较,也更适用于寄宿在Windows Service这种将当前目录视为System32的场合。


直接上配置文件。

<!--定义输出到PostgreSql数据库中,用于Windows Service这种无法正确识别程序当前目录从而无法使用相对路径的场合-->
    <appender name="AdoNetAppender_PostgreSQL_All" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="10"/>
      <connectionType value="Npgsql.NpgsqlConnection, Npgsql"/>
      <!--PostgreSql连接字符串-->
      <connectionString value="Server=127.0.0.1;Port=5432;User Id=postgres;Password=tm;Database=postgres;"/>
      <!--PostgreSql插入字符串-->
      <!--如果想在PostgreSql中保持字段字有大小写,要加入"
          但是由于我用了Ormlite,所以不需要了
      <commandText value="INSERT INTO "Log" 
                   ("AppDomain", "AspnetCache", "AspnetContext", "AspnetRequest", "AspnetSession", 
                   "Logger", "Date", "Identity", "Level", "Location", "Exception",
                   "Message", "Thread", "NDC", "StackTraceDetail", "UserName", "UTCDate")
                    VALUES
                    (:AppDomain, :AspnetCache, :AspnetContext, :AspnetRequest, :AspnetSession, :Logger, :Date, :Identity, :Level, :Location, :Exception,
                    :Message, :Thread, :NDC, :StackTraceDetail, :UserName, :UTCDate)"/>
      -->
      <!--为了配合Ormlite生成的表,中间加上下划线-->
            <commandText value="INSERT INTO log 
                   (app_domain, aspnet_cache, aspnet_context, aspnet_request, aspnet_session, 
                   logger, date_time, identity, level, location, exception,
                   message, thread, ndc, stack_trace_detail, user_name, utc_date)
                    VALUES
                    (:AppDomain, :AspnetCache, :AspnetContext, :AspnetRequest, :AspnetSession, :Logger, :Date, :Identity, :Level, :Location, :Exception,
                    :Message, :Thread, :NDC, :StackTraceDetail, :UserName, :UTCDate)"/>
      <parameter>
        <parameterName value="AppDomain"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%appdomain"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="AspnetCache"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-cache"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="AspnetContext"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-context"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="AspnetRequest"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-request"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="AspnetSession"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-session"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Logger"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
      </parameter>
      <parameter>
        <parameterName value="Identity"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%identity"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Level"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Location"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%location"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Exception"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.ExceptionLayout">
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Message"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="Thread"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="NDC"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%ndc"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="StackTraceDetail"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%stacktracedetail{10}"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="UserName"/>
        <dbType value="String"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%username"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="UTCDate"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawUtcTimeStampLayout"/>
      </parameter>
    </appender>

运行时当前目录下要有Npgsql.dll,还要事先在PgSQL中建好表,好人做到底,再奉上Ormlite生成的SQL:

-- Table: log

-- DROP TABLE log;

CREATE TABLE log
(
  id serial NOT NULL,
  date_time timestamp without time zone NOT NULL,
  thread text,
  level text,
  logger text,
  message text,
  exception text,
  stack_trace_detail text,
  location text,
  identity text,
  app_domain text,
  user_name text,
  ndc text,
  utc_date timestamp without time zone,
  aspnet_cache text,
  aspnet_context text,
  aspnet_request text,
  aspnet_session text,
  CONSTRAINT log_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE log
  OWNER TO postgres;

-- Index: idx_log_level

-- DROP INDEX idx_log_level;

CREATE INDEX idx_log_level
  ON log
  USING btree
  (level COLLATE pg_catalog."default");

-- Index: uidx_log_date

-- DROP INDEX uidx_log_date;

CREATE UNIQUE INDEX uidx_log_date
  ON log
  USING btree
  (date);


发完贴子才发现系统将&quot;给解析成了双引号,再直接贴上:

<!--如果想在PostgreSql中保持字段字有大小写,要加入&quot
          但是由于我用了Ormlite,所以不需要了
      <commandText value="INSERT INTO &quot;Log&quot; 
                   (&quot;AppDomain&quot;, &quot;AspnetCache&quot;, &quot;AspnetContext&quot;, &quot;AspnetRequest&quot;, &quot;AspnetSession&quot;, 
                   &quot;Logger&quot;, &quot;Date&quot;, &quot;Identity&quot;, &quot;Level&quot;, &quot;Location&quot;, &quot;Exception&quot;,
                   &quot;Message&quot;, &quot;Thread&quot;, &quot;NDC&quot;, &quot;StackTraceDetail&quot;, &quot;UserName&quot;, &quot;UTCDate&quot;)
                    VALUES
                    (:AppDomain, :AspnetCache, :AspnetContext, :AspnetRequest, :AspnetSession, :Logger, :Date, :Identity, :Level, :Location, :Exception,
                    :Message, :Thread, :NDC, :StackTraceDetail, :UserName, :UTCDate)"/>
      -->


你可能感兴趣的:(C#,database,PostgreSQL,log4net)