zoukankan      html  css  js  c++  java
  • 通过代码或者配置文件 对log4net进行配置

    1.通过代码进行配置

    1.1代码

    http://stackoverflow.com/questions/16336917/can-you-configure-log4net-in-code-instead-of-using-a-config-file

     public class Logger
        {
            public static void Setup()
            {
                Hierarchy hierarchy = (Hierarchy) LogManager.GetRepository();
    
                PatternLayout patternLayout = new PatternLayout();
                patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
                patternLayout.ActivateOptions();
    
                RollingFileAppender roller = new RollingFileAppender();
                roller.AppendToFile = false;
                roller.File = @"LogsEventLog.txt";
                roller.Layout = patternLayout;
                roller.MaxSizeRollBackups = 5;
                roller.MaximumFileSize = "1GB";
                roller.RollingStyle = RollingFileAppender.RollingMode.Size;
                roller.StaticLogFileName = true;
                roller.ActivateOptions();
                hierarchy.Root.AddAppender(roller);
    
                MemoryAppender memory = new MemoryAppender();
                memory.ActivateOptions();
                hierarchy.Root.AddAppender(memory);
    
                hierarchy.Root.Level = Level.Info;
                hierarchy.Configured = true;
            }
        }

     1.2代码调用

    1.2.1 在程序启动的时候,调用  Logger.Setup();

    1.2.2 输出日志

    private static void Application_ThreadException(object sender,ThreadExceptionEventArgs e)
            {
                var logger = LogManager.GetLogger(sender.GetType());
                logger.Error("Error", e.Exception);
                MessageBox.Show(e.Exception.Message);
            }

    2.通过配置文件进行配置 

    2.1配置文件内容

      <configSections>
        <section name ="log4net" type ="System.Configuration.IgnoreSectionHandler"/>
      </configSections>
      <log4net>
        <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="ERROR" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logserr.log" />
          <PreserveLogFileNameExtension value="true" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsinfo.log" />
          <PreserveLogFileNameExtension value="true" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="DEBUG" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsdebug.log" />
          <PreserveLogFileNameExtension value="true" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsperf.log" />
          <PreserveLogFileNameExtension value="true" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %logger - %message%newline" />
          </layout>
        </appender>
        <root>
          <level value="ALL" />
          <appender-ref ref="errorAppender" />
          <appender-ref ref="infoAppender" />
          <appender-ref ref="debugAppender" />
        </root>
        <logger name="Performance" additivity="false">
          <level value="ALL" />
          <appender-ref ref="perfAppender" />
        </logger>
      </log4net>

    2.2代码调用 

    http://www.cnblogs.com/chucklu/p/4359582.html

    需要注意

    XmlConfigurator.Configure();这个不带参数的用法,需要研究一下

  • 相关阅读:
    spring-webmvc 4.3.4 与 freemarker、easyui 整合
    CentOS 7 网络配置
    CentOS 7 安装 mariaDB
    CentOS 7 安装 tomcat7.0
    CentOS 7 安装JDK
    利用icepdf将pdf文件转为图片
    jwplayer 网页在线播放插件
    postgresql 行转列,拼接字符串
    activemq-5.13 在windows下部署应用
    几款Java常用基础工具库
  • 原文地址:https://www.cnblogs.com/chucklu/p/5404813.html
Copyright © 2011-2022 走看看