WPF 如何在单独的配置文件中使用Log4net

WPF 如何在单独的配置文件中使用Log4net

引用log4net包

NuGet程序包 -----》搜索log4net -------》点击安装

创建单独的配置文件

  1. log4net.config
  2. 文件内容(日志文件放在DEBUG)
<log4net>

	<root>
		<level value="DEBUG" />
		<appender-ref ref="DebugRollingFileAppender" />
		<appender-ref ref="InfoRollingFileAppender" />
		<appender-ref ref="WarnRollingFileAppender" />
		<appender-ref ref="ErrorRollingFileAppender" />
	</root>

	<appender name="DebugRollingFileAppender"
			  type="log4net.Appender.RollingFileAppender">
		<file value="Logs/Debug/" />
		<appendToFile value="true" />
		<immediateFlush value="true" />
		<staticLogFileName value="false" />
		<rollingStyle value="Date" />
		<datePattern value="yyyy-MM-dd\\yyyy-MM-dd_HH&quot;.log&quot;" />
		<MaximumFileSize value="50MB" />
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.ffff} [%t] %c :%newline %m%n %newline " />
		</layout>
		<filter type="log4net.Filter.LevelMatchFilter">
			<levelToMatch  value="DEBUG" />
		</filter>
		<filter type="log4net.Filter.DenyAllFilter" />
	</appender>

	<appender name="InfoRollingFileAppender"
			  type="log4net.Appender.RollingFileAppender">
		<file value="Logs/Info/" />
		<appendToFile value="true" />
		<immediateFlush value="true" />
		<staticLogFileName value="false" />
		<rollingStyle value="Date" />
		<datePattern value="yyyy-MM-dd\\yyyy-MM-dd_HH&quot;.log&quot;" />
		<MaximumFileSize value="50MB" />
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.ffff} %p{NDC}:%newline %m%n" />
		</layout>
		<filter type="log4net.Filter.LevelMatchFilter">
			<levelToMatch  value="INFO" />
		</filter>
		<filter type="log4net.Filter.DenyAllFilter" />
	</appender>

	<appender name="WarnRollingFileAppender"
			  type="log4net.Appender.RollingFileAppender">
		<file value="Logs/Warn/" />
		<appendToFile value="true" />
		<immediateFlush value="true" />
		<staticLogFileName value="false" />
		<rollingStyle value="Date" />
		<datePattern value="yyyy-MM-dd\\yyyy-MM-dd_HH&quot;.log&quot;" />
		<MaximumFileSize value="50MB" />
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.ffff} %p{NDC}:%newline %m%n" />
		</layout>
		<filter type="log4net.Filter.LevelMatchFilter">
			<levelToMatch  value="WARN" />
		</filter>
		<filter type="log4net.Filter.DenyAllFilter" />
	</appender>

	<appender name="ErrorRollingFileAppender"
			  type="log4net.Appender.RollingFileAppender">
		<file value="Logs/Error/" />
		<appendToFile value="true" />
		<immediateFlush value="true" />
		<staticLogFileName value="false" />
		<rollingStyle value="Date" />
		<datePattern value="yyyy-MM-dd\\yyyy-MM-dd_HH&quot;.log&quot;" />
		<MaximumFileSize value="50MB" />
		<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.ffff} %p{NDC}:%newline %m%n" />
		</layout>
		<filter type="log4net.Filter.LevelMatchFilter">
			<levelToMatch  value="ERROR" />
		</filter>
		<filter type="log4net.Filter.DenyAllFilter" />
	</appender>
</log4net>

使用log4

在要用到log4的地方命名空间上边加上[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Config\log4net.config", Watch = true)]

namespace SlicingMachine
{
    public partial class App : Application
    {
        public static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected override void OnStartup(StartupEventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            base.OnStartup(e);
            log.Info("==Startup=====================>>>");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            log.Info("<<<========================End==");
            base.OnExit(e);
        }
    }
}
public void SendData(int index = 0)
{
    App.log.Debug("发生错误==============================");

}
posted @ 2021-07-06 17:16  AJun816  阅读(181)  评论(0编辑  收藏  举报