log4Net 写入txt文件 配置 & log4Net控制台输出

=》写入文件  Config

config文件


<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>


  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>


  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="pageCount" value="33" />
    <add key="JsonIngredientDoc" value="c:\db\ingredient_v3.json" />
    <add key="instagram_AccessToken" value="1508910419.48a00b7.916bf47cd7fd41af9223593458d06114" />
  </appSettings>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="RollingLogFileAppender_DateFormat_log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread](%file:%line) %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
</configuration>


CS里面


public log4net.ILog log = log4net.LogManager.GetLogger("logger-name");

log.Info("info")

log.Error("error")


然后最重要的一步:

 log4net.Config.XmlConfigurator.Configure();

在你项目启动的时候,  global.=:>application_start()  或者main()方法里:加上上面这句话


=》控制台输出  Config

<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="Info" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
      </layout>

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="Info" />
        <param name="LevelMax" value="Fatal" />
      </filter>
    </appender>
 <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="all" />
      <appender-ref ref="ColoredConsoleAppender"/>
    </root>



你可能感兴趣的:(log4Net 写入txt文件 配置 & log4Net控制台输出)