zoukankan      html  css  js  c++  java
  • log4net更换目录

     public static void ChangeRollingFileName()
            {
                // ログファイル
                log4net.ILog iLog = log4net.LogManager.GetLogger("AppLogger");
                // パス
                string strPath = Environment.GetEnvironmentVariables()["USERPROFILE"].ToString() + "\\log\\";
                string strFilename = strPath + "log.txt";
                // 文件夹生成
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                log4net.Core.LogImpl logImpl = iLog as log4net.Core.LogImpl;
                if (logImpl != null)
                {
                    log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logImpl.Logger).Appenders;
                    for (int i = 0; i < ac.Count; i++)
                    {  
                        //
                        log4net.Appender.FileAppender rfa = ac[i] as log4net.Appender.FileAppender;
                        if (rfa != null)
                        {
                            rfa.File = strFilename;
                            // 写文件
                            rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
                        }
                    }
                }

    如何调用:===================================================================
    调用class
    public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
     
    private void TestChangeLog4netLogFileName()
            {
                ChangeLog4netLogFileName();
                iLog.Info("Test:info");
            }
     
    configue 的配置===================================================================
    <configuration>
      <log4net>
        <!-- Define some output appenders -->
        <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
          <param name="File" value="ClientLog.txt"/>
          <param name="AppendToFile" value="true"/>
          <param name="MaxSizeRollBackups" value="10"/>
          <param name="MaximumFileSize" value="5MB"/>
          <param name="RollingStyle" value="Size"/>
          <param name="StaticLogFileName" value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %u %-5p [%class][%method] - %m%n"/>
          </layout>
         
          <!-- Alternate layout using XML    
        <layout type="log4net.Layout.XMLLayout" /> -->
        </appender>
        <!-- Setup the root category, add the appenders and set the default level -->
        <root>
          <level value="ALL" />
          <appender-ref ref="LogFileAppender" />
          <!-- <appender-ref ref="A" /> -->
        </root>
        <logger name="AppLogger">
          <level value="ALL" />
          <appender-ref ref="LogFileAppender" />
        </logger>
        <logger name="Form1">
          <level value="DEBUG" />
          <appender-ref ref="LogFileAppender" />
        </logger>

      </log4net>
    </configuration>
     
  • 相关阅读:
    Privacy Policy
    privacy
    将一个无法一次读入内存的大文件排序
    java实现二叉树的非递归遍历
    java静态方法同步问题
    关于java静态方法继承问题
    Android 代码写布局
    Android自定义ImageView实现手势放大图片的控件,无需依赖任何第三方。
    Gradle版本更换问题
    Android的AlertDialog实现圆角边框
  • 原文地址:https://www.cnblogs.com/wcLT/p/2738843.html
Copyright © 2011-2022 走看看