原因:
1. 没有载入配置文件
2. 配置文件没有选择“始终复制” or “如果较新则复制”

正常流程如下:

  1. 复制Dll 添加引用 或 NuGet 下载
  2. 添加Config文件 XML格式
点击查看代码


<?xml version="1.0" encoding="utf-8" ?>

<!-- WP 该配置文件可以事先在任意目录下新建并填入数据
    之后,添加现有文件(该配置文件)到项目中。然后鼠标右键该配置文件,在属性框的“复制到输出目录”选择输出-->
<configuration>

  <configSections>

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

  <log4net>

    <root>
      <priority value="ALL"/>
      <!--<appender-ref ref="InfoRollingFileAppender"/>-->
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="ErrorRollingFileAppender"/>
      <appender-ref ref="FatalErrorFileAppender"/>
    </root>

    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date  %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <datePattern value="(yyyyMMdd hh:mm:ss)"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date  %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>

    <!--全部日志-->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\VCLog\\log\\All.log"/>
      <param name="Encoding" value="utf-8" />
      <appendToFile value="true"/>
      <maxSizeRollBackups value="2"/>
      <maximumFileSize value="2000KB"/>
      <rollingStyle value="Size"/>
      <staticLogFileName value="true"/>

      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d |  %5p  | - %m%n"/>
      </layout>
    </appender>


    <!--信息日志-->
    <!--

      <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log\\Warning.log"/>
        <appendToFile value="true"/>
        <maxSizeRollBackups value="0"/>
        <maximumFileSize value="2000KB"/>
        <rollingStyle value="Size"/>
        <staticLogFileName value="true"/>
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%d | - %m%n"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
          <param name="LevelMin" value="WARN" />
          <param name="LevelMax" value="WARN" />
        </filter>
      </appender>-->

    <!--错误日志-->

    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\VCLog\\log\\Error.log"/>
      <param name="Encoding" value="utf-8" />
      <appendToFile value="true"/>
      <maxSizeRollBackups value="1"/>
      <maximumFileSize value="2000KB"/>
      <rollingStyle value="Size"/>
      <staticLogFileName value="true"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d | %-5p  | -%m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="Error" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>


    <appender name="FatalErrorFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\VCLog\\log\\Fatal.log"/>
      <param name="Encoding" value="utf-8" />
      <appendToFile value="true"/>
      <maxSizeRollBackups value="1"/>
      <maximumFileSize value="8000KB"/>
      <rollingStyle value="Size"/>
      <staticLogFileName value="true"/>
      <datePattern value="(yyyyMMdd )"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d | %-5p  | -%m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="FATAL" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>

  </log4net>
</configuration>
  1. 程序启动处,载入Log4Net ** 配置载入** 和 初始化 流程
            //Log4Net Process
            FileInfo fileInfo = new FileInfo(@"Config\AppLog.config");
            LogBase.Init(fileInfo);
            LogBase.Write("程序界面启动", LogBase.LogMessageType.Info);

4.添加Log4Net 操作类

点击查看代码




  public class LogBase
  {
      private LogBase() { }
      private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.
      private static ILog m_log;
      /// <summary>
      /// 初始化日志系统
      /// 在系统运行开始初始化
      /// Global.asax Application_Start内
      /// 在使用init方法时,需要新建FileInfo的实例,并将文件名输入 new Fileinfo(“文件名.扩展名”)
      /// </summary>
      public static void Init(System.IO.FileInfo fileInfo)
      {
          //如果是下面一行的代码,那么log4net 会在项目的App.config中寻找log的配置信息
          //log4net.Config.XmlConfigurator.Configure();
          //如果是下面一行的代码,那么log4net会在生成目录中寻找配置文件,配置文件信息需要传入Init函数的参数中
          log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);

      }
      /// <summary>
      /// 写入日志
      /// </summary>
      /// <param name="message">日志信息</param>
      /// <param name="messageType">日志类型</param>
      public static void Write(string message, LogMessageType messageType)
      {
          DoLog(message, messageType, null, Type.GetType("System.Object"));
      }
      /// <summary>
      /// 写入日志
      /// </summary>
      /// <param name="message">日志信息</param>
      /// <param name="messageType">日志类型</param>
      /// <param name="type"></param>
      public static void Write(string message, LogMessageType messageType, Type type)
      {
          DoLog(message, messageType, null, type);
      }
      /// <summary>
      /// 写入日志
      /// </summary>
      /// <param name="message">日志信息</param>
      /// <param name="messageType">日志类型</param>
      /// <param name="ex">异常</param>
      public static void Write(string message, LogMessageType messageType, Exception ex)
      {
          DoLog(message, messageType, ex, Type.GetType("System.Object"));
      }
      /// <summary>
      /// 写入日志
      /// </summary>
      /// <param name="message">日志信息</param>
      /// <param name="messageType">日志类型</param>
      /// <param name="ex">异常</param>
      /// <param name="type"></param>
      public static void Write(string message, LogMessageType messageType, Exception ex, Type type)
      {
          DoLog(message, messageType, ex, type);
      }
      /// <summary>
      /// 断言
      /// </summary>
      /// <param name="condition">条件</param>
      /// <param name="message">日志信息</param>
      public static void Assert(bool condition, string message)
      {
          Assert(condition, message, Type.GetType("System.Object"));
      }
      /// <summary>
      /// 断言
      /// </summary>
      /// <param name="condition">条件</param>
      /// <param name="message">日志信息</param>
      /// <param name="type">日志类型</param>
      public static void Assert(bool condition, string message, Type type)
      {
          if (condition == false)
              Write(message, LogMessageType.Info);
      }
      /// <summary>
      /// 保存日志
      /// </summary>
      /// <param name="message">日志信息</param>
      /// <param name="messageType">日志类型</param>
      /// <param name="ex">异常</param>
      /// <param name="type">日志类型</param>
      private static void DoLog(string message, LogMessageType messageType, Exception ex, Type type)
      {
          m_log = LogManager.GetLogger(type);
          switch (messageType)
          {
              case LogMessageType.Debug:
                  LogBase.m_log.Debug(message, ex);
                  break;
              case LogMessageType.Info:
                  LogBase.m_log.Info(message, ex);
                  break;
              case LogMessageType.Warn:
                  LogBase.m_log.Warn(message, ex);
                  break;
              case LogMessageType.Error:
                  LogBase.m_log.Error(message, ex);
                  break;
              case LogMessageType.Fatal:
                  LogBase.m_log.Fatal(message, ex);
                  break;
          }
      }
      /// <summary>
      /// 日志类型
      /// </summary>
      public enum LogMessageType
      {
          /// <summary>
          /// 调试
          /// </summary>
          Debug,
          /// <summary>
          /// 信息
          /// </summary>
          Info,
          /// <summary>
          /// 警告
          /// </summary>
          Warn,
          /// <summary>
          /// 错误
          /// </summary>
          Error,
          /// <summary>
          /// 致命错误
          /// </summary>
          Fatal
      }
  }

  1. Log4Net 的实现

      `    LogBase.Write("数据库访问失败,请检查数据库状态及连接", LogBase.LogMessageType.Error);`