C#日志系统框架Log4Net
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/580d4fcc82a7894a7c98c8dafd1bb528.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2af0dbedd7571b148834e38259fb4f2f.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fa14a30d8829562e62a3165c51edcf80.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0b793e1417f4a58c2f39eb09ab76b38e.png)
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<log4net>
<!--配置日志的目标存储形式-->
<root>
<!--输出等级-->
<level value="ALL" />
<!--控制台形式-->
<appender-ref ref="ConsoleAppender" />
<!--文件形式-->
<appender-ref ref = "FileAppender" />
</root>
<!--控制台形式-->
<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/log-file.txt" />
<!--是否追加-->
<appendToFile value = "true" />
<!--显示格式-->
<layout type = "log4net.Layout.PatternLayout" >
<conversionPattern value = "%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout >
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
</appender>
</log4net>
</configuration>
将log4net属性设置为始终复制
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1bda925053b55d44eb8e5426e3e416b8.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6830abc8f498184d84946a234ad39c0d.png)
在 AssemblyInfo.cs 中添加一行代码
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3b59e38039f2018e1893b1b36ea0eaa0.png)
public class LogHelper
{
#region 记录日志
public static void LoggerMsg(LogLevel legLevel, string msg)
{
log4net.ILog log = GetLogger();
switch (legLevel)
{
case LogLevel.Debug: log.Debug(msg); break;
case LogLevel.Error: log.Error(msg); break;
case LogLevel.Fatal: log.Fatal(msg); break;
case LogLevel.Info: log.Info(msg); break;
case LogLevel.Warn: log.Warn(msg); break;
default: break;
}
}
#endregion
#region 获得一个日志记录对象
public static log4net.ILog GetLogger()
{
return log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
}
#endregion
}
public enum LogLevel
{
Debug,
Info,
Warn,
Error,
Fatal,
}
public FrmMain()
{
InitializeComponent();
LogHelper.LoggerMsg(LogLevel.Info, "hello,log4net!");
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f30f5054a9ac0f8265cbc2008acede21.png)