C#, .Net添加log4net日志功能

相关的视频教程 https://www.bilibili.com/video/BV1664y1C7ko/ (如果不会请看我录制的视频教程)
log4net.config配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler,&#xD;&#xA;log4net-net-1.0"/>
  </configSections>
  <log4net>
	<appender name="AppLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="SysLog\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="-1" />
      <!--最小锁定模型以允许多个进程可以写入同一个文件-->
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="RollingStyle" value="Size" />
      <param name="DatePattern" value="yyyy-MM-dd" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p %d [%c] %m%n" />
      </layout>
    </appender>
    <logger name="AppLog">
      <level value="all" />
      <appender-ref ref="AppLog" />
    </logger>	
    <root>
      <level value="all" />
    </root>
  </log4net>
</configuration>
log4net类
public class AppLog
{
    private static string filepath = AppDomain.CurrentDomain.BaseDirectory + @"\SysLog\";

    private static readonly log4net.ILog logComm = log4net.LogManager.GetLogger("AppLog");

    static AppLog()
    {
        log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));

        if (!Directory.Exists(filepath))
        {
            Directory.CreateDirectory(filepath);
        }
    }

    /// <summary>
    /// 输出系统日志
    /// </summary>
    /// <param name="msg">信息内容</param>
    /// <param name="source">信息来源</param>
    private static void WriteLog(string msg, bool isWrite, Action<object> action)
    {
        if (isWrite)
        {
            string filename = "AppLog_" + DateTime.Now.ToString("yyyyMMdd_HH") + ".log";
            var repository = LogManager.GetRepository();

            #region MyRegion
            var appenders = repository.GetAppenders();
            if (appenders.Length > 0)
            {
                RollingFileAppender targetApder = null;
                foreach (var Apder in appenders)
                {
                    if (Apder.Name == "AppLog")
                    {
                        targetApder = Apder as RollingFileAppender;
                        break;
                    }
                }
                if (targetApder.Name == "AppLog")//如果是文件输出类型日志,则更改输出路径
                {
                    if (targetApder != null)
                    {
                        if (!targetApder.File.Contains(filename))
                        {
                            targetApder.File = @"SysLog\" + filename;
                            targetApder.ActivateOptions();
                        }
                    }
                }
            }

            #endregion
            action(msg);
            //logComm.Error(msg + "\n");
        }
    }
    public static void WriteError(string msg, bool isWrite)
    {
        WriteLog(msg, isWrite, logComm.Error);
    }
    public static void WriteInfo(string msg, bool isWrite)
    {
        WriteLog(msg, isWrite, logComm.Info);
    }
    public static void WriteWarn(string msg, bool isWrite)
    {
        WriteLog(msg, isWrite, logComm.Warn);
    }
}

评论

  1. 111.0.126.*2021-11-12 13:48:27
    如何把这个日志文件映射到数据库表中啊  百度了好多  都实现不了

  2. 36.113.113.*2021-08-19 01:06:54
    求解

  3. 36.113.113.*2021-08-19 01:06:45
    如何把这个日志文件映射到数据库表中啊