log4net配置文件

log4net配置文件

Log4Net.config


AssemblyInfo.cs文件中添加

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]


log4net配置文件_第1张图片



<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <log4net>
        <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="log/"/>
            <param name="AppendToFile" value="true"/>
            <param name="StaticLogFileName" value="false"/>
            <param name="DatePattern" value="yyyy-MM/yyyy-MM-dd&quot;.txt&quot;" />
            <param name="RollingStyle" value="Composite"/>
            <layout type="log4net.Layout.PatternLayout,log4net">
                <param name="ConversionPattern" value="%newline%d [%t] %-5p %c [%x] - %m%n%newline"/>
            </layout>
            <!--<filter type="log4net.Filter.LevelRangeFilter">
                <param name="AcceptOnMatch" value="true"/>
                <param name="LevelMin" value="DEBUG"/>
                <param name="LevelMax" value="INFO"/>
            </filter>-->
        </appender>
        <appender name="RollingFile2" type="log4net.Appender.RollingFileAppender">

            <param name="File" value="log\\Log_"/>
            <param name="AppendToFile" value="true"/>
            <param name="StaticLogFileName" value="false"/>
            <param name="DatePattern" value="yyyyMMdd".log""/>
            <param name="RollingStyle" value="Date"/>
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%newline%d [%t] %-5p %c [%x] - %m%n%newline"/>
            </layout>
        </appender>
        <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
            <authentication value="Basic" />
            <!--多个邮箱用“,”分开。如:[email protected],[email protected]>
            <to value="接收邮件@qq.com" />
            <from value="发送邮件@163.com" />
            <username value="用户名" />
            <password value="密码" />
            <subject value="主题" />
            <smtpHost value="smtp.163.com" />
            <bufferSize value="1" />
            <lossy value="true" />
            <evaluator type="log4net.Core.LevelEvaluator">
                <threshold value="DEBUG"/>
            </evaluator>
            <layout type="log4net.Layout.PatternLayout,log4net">
                <param name="ConversionPattern" value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
            </layout>
            <filter type="log4net.Filter.LevelRangeFilter">
                <param name="AcceptOnMatch" value="true"/>
                <param name="LevelMin" value="WARN"/>
                <!--<param name="LevelMax" value="INFO"/>-->
            </filter>
        </appender>

        <root>
            <priority value="ALL" />
            <appender-ref ref="SmtpAppender" />
            <appender-ref ref="RollingFile" />
            <!--<appender-ref ref="RollingFile2" />-->

        </root>
    </log4net>
</configuration>


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <!--注意事项:1.计算机名称不能为中文 2.每天的邮箱有数量限制 3.当前log4net版本为1.2.10-->
  <log4net>
    <!--配置发送邮件-->
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <authentication value="Basic" />
      <to value="[email protected]" />
      <from value="[email protected]" />
      <username value="12" />
      <password value="12" />
      <subject value="log4net测试邮箱通讯" />
      <smtpHost value="smtp.163.com" />
      <bufferSize value="1" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="DEBUG"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
      </layout>
    </appender>

    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="log/"/>
      <param name="AppendToFile" value="true"/>
      <param name="RollingStyle" value="Composite"/>
      <param name="DatePattern" value="yyyy-MM/yyyy-MM-dd" />
      <param name="StaticLogFileName" value="false"/>
      <param name="maximumFileSize" value="4MB"/>
      <param name="CountDirection" value="1"/>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="AcceptOnMatch" value="true"/>
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="INFO"/>
      </filter>
    </appender>

    <!--配置写入数据库-->
    <appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
      <!--定义几条存入数据库 有2条时才写入数据库,没有两条叫缓存-->
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source=192.168.1.253;Initial Catalog=Log4net;User ID=sa;password=123456;Persist Security Info=True;" />
      <!--存储过程-->
      <param name="CommandType" value="StoredProcedure" />
      <param name="CommandText" value="[Up_Manager_InsertLog]" />
      <parameter>
        <parameterName value="@Date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@Message" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%Message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@StackTrace" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%StackTrace" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@UserName" />
        <dbType value="String"/>
        <size value="50"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%UserName" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Domain" />
        <dbType value="String"/>
        <size value="200"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%Domain" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@ErrorCode" />
        <dbType value="String"/>
        <size value="20"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%ErrorCode" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Ip" />
        <dbType value="String"/>
        <size value="20"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%Ip" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@SQL" />
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%SQL" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@EntityName" />
        <dbType value="String"/>
        <size value="50"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%EntityName" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Handle" />
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%Handle" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Mark" />
        <dbType value="String"/>
        <size value="1"/>
        <layout type="FastGuard.Popedom.Log.CustomLayout,FastGuard.Popedom.Log">
          <conversionPattern value="%Mark" />
        </layout>
      </parameter>
    </appender>

    <!--启动哪些附着器-->
    <root>
      <!--<priority value="ALL" />
			<appender-ref ref="SmtpAppender" />
      <appender-ref ref="RollingFile" />-->
      <appender-ref ref="ADONetAppender" />
    </root>
  </log4net>
</configuration>







你可能感兴趣的:(log4net配置文件)