log4net basic example - write to file

 

<?xml version="1.0"?>

<configuration>

  <configSections>

    <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>

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

  </configSections>



  <log4net>

    <logger name="loginfo">

      <level value="ALL"/>

      <appender-ref ref="InfoAppender"/>

    </logger>

    

    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">

      <param name="File" value="Log\\test.log"/>

      <param name="AppendToFile" value="false"/>

      <param name="MaximumFileSize" value="1KB"/>

      <param name="MaxSizeRollBackups" value="100"/>

      <param name="DatePattern" value="yyyy-MM-dd"/>

      <param name="RollingStyle" value="Date"/>

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />

      </layout>

    </appender>



    <!-- Set root logger level to DEBUG and its only appender to A1 --><!--

    <root>

      <level value="ALL"/>

      <appender-ref ref="InfoAppender"/>

    </root>-->

  </log4net>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
using log4net;

using log4net.Config;



namespace TestLog4Net

{

    class Program

    {

        static readonly ILog logger = LogManager.GetLogger("loginfo");

        static void Main(string[] args)

        {

            try

            {

                log4net.Config.XmlConfigurator.Configure();

                logger.Debug("Here is a debug log.");

                logger.Info("... and an Info log.");

                logger.Warn("... and a warning.");

                logger.Error("... and an error.");

                logger.Fatal("... and a fatal error.");

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

                Console.ReadLine();

            }

        }

    }

}

你可能感兴趣的:(example)