c#使用log4Net配置日志文件
1.# 写一个通用类 LogHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
namespace WindowsFormsApplication22
{
public class LogHelper
{
static ILog mylog = LogManager.GetLogger(typeof(LogManager));
//创建列表,存储日志信息
public static List<Record> fatal_list = new List<Record>();
public static List<Record> error_list = new List<Record>();
public static List<Record> info_list = new List<Record>();
public static List<Record> debug_list = new List<Record>();
public static List<Record> warning_list = new List<Record>();
//报警等级分5 个
public static void WriteFitle(object obj)
{
mylog.Fatal(obj);
fatal_list.Add(new Record() { Id=fatal_list.Count+1,Name=obj.ToString(),datatime=DateTime.Now});
}
public static void WriteError(object obj)
{
mylog.Error(obj);
error_list.Add(new Record() { Id = error_list.Count, Name = obj.ToString(), datatime = DateTime.Now });
}
public static void WriteInfo(object obj)
{
mylog.Info(obj);
info_list.Add(new Record() { Id = info_list.Count, Name = obj.ToString(), datatime = DateTime.Now });
}
public static void WriteDebug(object obj)
{
mylog.Debug(obj);
debug_list.Add(new Record() { Id=debug_list.Count,Name=obj.ToString(),datatime=DateTime.Now});
}
public static void WriteWarning(object obj)
{
mylog.Warn(obj);
warning_list.Add(new Record() { Id = warning_list.Count, Name = obj.ToString(), datatime = DateTime.Now });
}
}
public class Record
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime datatime { get; set; }
}
}
2.配置文件信息 App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%date{yyyy--MM--dd}.log"/>
<appendToFile value="true" />
<maximumFileSize value="5KB"></maximumFileSize>
<maxSizeRollBackups values="5"></maxSizeRollBackups>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="日志时间: %date 线程:[%thread] 日志级别:%-5level 记录信息: %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
3界面测试 ,首先加载界面配置文件:
log4net.Config.XmlConfigurator.Configure(); //加载配置文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure(); //加载配置文件
LogHelper.WriteFitle("致命错误1");
LogHelper.WriteFitle("致命错误2");
LogHelper.WriteFitle("致命错误3");
LogHelper.WriteFitle("致命错误4");
LogHelper.WriteFitle("致命错误5");
LogHelper.WriteInfo("普通一条信息1");
LogHelper.WriteInfo("普通一条信息2");
LogHelper.WriteInfo("普通一条信息3");
LogHelper.WriteError("错误信息1");
LogHelper.WriteError("错误信息2");
LogHelper.WriteError("错误信息3");
LogHelper.WriteError("错误信息3");
LogHelper.WriteDebug("Debug 输出1");
LogHelper.WriteDebug("Debug 输出2");
LogHelper.WriteDebug("Debug 输出3");
LogHelper.WriteWarning("提示信息1");
LogHelper.WriteWarning("提示信息2");
LogHelper.WriteWarning("提示信息3");
LogHelper.WriteWarning("提示信息4");
}
private void buttonDebug_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = LogHelper.debug_list;
}
private void buttonWarning_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = LogHelper.warning_list;
}
private void buttonInfo_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = LogHelper.info_list;
}
private void buttonFatals_Click(object sender, EventArgs e)
{
this.dataGridView1.DataSource = LogHelper.fatal_list;
}
}
}
运行效果: