Oracle中EnterPrise Library+Log4Net测试~

配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <connectionStrings>
    <add name="testdb" connectionString="Data Source=test;Persist Security Info=True;User ID=system;Password=manager;Unicode=True" providerName="System.Data.OracleClient"/>
  </connectionStrings>
  <log4net>
    <appender name="LogAllToFile" type="log4net.Appender.RollingFileAppender,log4net">
      <!--输出格式
                     每种转换符号都以%开始,后面跟着一个格式符号和换符号。
                     %-数字 :该项的最小长度,小于最小长度的用空格填充
                     %m(message):输出的日志消息
                     %n(new line):换行
                     %d(datetime):输出当前语句运行的时刻
                     %r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
                     %t(thread id):当前语句所在的线程ID
                     %p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
                     %c(class):当前日志对象的名称,
                     %L(line ):输出语句所在的行号
                     %F(file name):输出语句所在的文件名
                     %logger 日志名称
                 -->
      <param name="File" value="log\"/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <param name="MaximumFileSize" value="1KB"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="yyyyMMdd&quot;.log&quot;"/>
      <param name="RollingStyle" value="Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%property{Operator} 操作类型:%property{ActionType}%n当前机器名:%property%n当前机器名及登录用户:%username %n记录位置:%location%n消息描述:%property{Message}%n异常:%exception%n消息:%message%newline%n%n" />
      </layout>
    </appender>
 
    <!--记录到文件方式-->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="RollingFileAppender_log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <appender name="RollingLogFileAppender_DateFormat" type="log4net.Appender.RollingFileAppender">
      <file value="RollingLogFileAppender_DateFormat_log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <!--<datePattern value="yyyyMMdd-HHmm" />-->
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread](%file:%line) %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
 
    <!--记录到oracle数据库方式-->
    <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
      <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=test;Persist Security Info=True;User ID=system;Password=manager;" />
      <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
      <bufferSize value="1" />
      <parameter>
        <parameterName value=":log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value=":thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value=":message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
    </appender>
   
    <!--记录到SQLite数据库方式-->
    <appender name="AdoNetAppender_SQLite" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.66.0, Culture=neutral" />
      <!--SQLite连接字符串-->
      <connectionString value="Data Source=c:\\log4net.db;Version=3;" />
      <commandText value="INSERT INTO Log (Date, Level, Logger,Source, Message) VALUES (@Date, @Level, @Logger, @Source, @Message)" />
      <parameter>
        <parameterName value="@Date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@Level" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Logger" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Source" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%file:%line" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Message" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
    </appender>
   
    <root>
      <!--文件形式记录日志-->
      <appender-ref ref="LogAllToFile" />
      <!--控制台控制显示日志-->
      <appender-ref ref="ConsoleAppender" />
      <!--Windows事件日志-->
      <appender-ref ref="EventLogAppender" />
      <!--SQLite事件日志-->
      <appender-ref ref="AdoNetAppender_SQLite" />
      <!--RollingFileAppender事件日志-->
      <appender-ref ref="RollingFileAppender" />
      <!--RollingFileAppender事件日志,每天一个日志-->
      <appender-ref ref="RollingLogFileAppender_DateFormat" />
      <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉 -->
      <appender-ref ref="AdoNetAppender_Oracle" />
    </root>
  </log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

调用:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]  

public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

log.Info("test");

log.Error(ex.message,ex);

posted @ 2011-03-29 22:45  AriLee  Views(555)  Comments(0Edit  收藏  举报