C#/.net 5.0 - log4net

引用包
  • log4net
log4.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>

	<!-- This section contains the log4net configuration settings -->
	<log4net>
		<!--<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
			<mapping>
				<level value="ERROR" />
				<foreColor value="Red, HighIntensity" />
			</mapping>
			<mapping>
				<level value="Warn" />
				<foreColor value="Yellow,HighIntensity" />
			</mapping>
			<mapping>
				<level value="Info" />
				<foreColor value="Green" />
			</mapping>
			<mapping>
				<level value="Debug" />
				<foreColor value="Purple, HighIntensity" />
			</mapping>
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %-5level - %message%newline" />
			</layout>
		</appender>-->
		<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
			<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
		</appender>
		<appender name="FileAppender" type="log4net.Appender.FileAppender">
			<file value="log-file.log" />
			<appendToFile value="true" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
			</layout>
		</appender>
		<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
			<file value="Log/Business/" />
			<appendToFile value="true" />
			<rollingStyle value="Composite" />
			<RollingStyle value="Date" />
			<staticLogFileName value="false" />
			<datePattern value="yyyy.MM.dd‘.log‘" />
			<!--<maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />-->
			<layout type="log4net.Layout.PatternLayout,log4net">
				<ConversionPattern value="%d [%t] %-5p %c - %m%n" />
				<Header value="
----------------------Header--------------------------
" />
				<Footer value="
----------------------Footer--------------------------
" />
			</layout>
		</appender>
		<!-- Setup the root category, add the appenders and set the default level -->
		<root>
			<level value="ALL" />
			<appender-ref ref="ConsoleAppender" />
			<!--<appender-ref ref="FileAppender" />-->
			<appender-ref ref="RollingLogFileAppender" />
		</root>
	</log4net>
</configuration>
控制台:LogHelper.cs
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;

namespace Business.Analysis.Common
{
    public class LogHelper
    {
        private static ILoggerRepository repository { get; set; }
        private static ILog _log;
        private static ILog log
        {
            get
            {
                if (_log == null)
                {
                    Configure();
                }
                return _log;
            }
        }

        public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
        {
            repository = LogManager.CreateRepository(repositoryName);
            XmlConfigurator.Configure(repository, new FileInfo(configFile));
            _log = LogManager.GetLogger(repositoryName, "RollingLogFileAppender");
        }
        public static void Info(string msg)
        {
            log.Info(msg);
        }

        public static void Warn(string msg)
        {
            log.Warn(msg);
        }

        public static void Error(string msg, Exception exception = null)
        {
            log.Error(msg, exception);
        }

        public static void Debug(string msg)
        {
            log.Debug(msg);
        }

        public static void Fatal(string msg)
        {
            log.Fatal(msg);
        }
    }
}
.net 5.0 中间件形式

 

 

 

 

 

C#/.net 5.0 - log4net

上一篇:Start Your Impossible by Toyoda


下一篇:终于找到了这个原文