一、简介
log4net是一款优秀的第三方日志框架,可以很容易的加载到开发项目中(引用log4net的dll,再配置些基本参数即可),帮助程序员把日志信息输出到各种不同的目标,常见的有文本、数据库、windows日志、邮件、内存缓冲区等目标。
二、配置文件
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!-- 错误日志类-->
<logger name="logerror">
<level value="ALL" />
<appender-ref ref="ErrorAppender" />
</logger>
<!-- 信息日志类 -->
<logger name="loginfo">
<level value="ALL" />
<appender-ref ref="InfoAppender" />
</logger>
<!-- 错误日志附加介质-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogError\\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".htm"" />
<param name="RollingStyle" value="Date" />
<!--布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
<!-- 信息日志附加介质-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogInfo\\" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".htm"" />
<param name="RollingStyle" value="Date" />
<!-- 信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
</log4net>
三、Logger 用法
1.新建项目,引入nuget包
新建一个C# 控制台项目,我这里用的 .Net8,安装log4net插件
2.配置文件的设置
同二
3.日志帮助类
接下来,我们添加代码,新建一个 LogHelp.cs ,用来控制日志的输出类型,比如:正常日志,错误日志
using log4net.Config;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Log4Demo
{
public class LogHelp
{
private static readonly ILog loginfo = LogManager.GetLogger("loginfo");
/// <summary>
/// 从缺省配置文件获取日志配置
/// </summary>
public static void SetConfig()
{
XmlConfigurator.Configure();
}
/// <summary>
/// 从指定配置文件获取日志配置
/// </summary>
/// <param name="configFile">指定的配置文件</param>
public static void SetConfig(FileInfo configFile)
{
XmlConfigurator.Configure(configFile);
}
/// <summary>
/// 生成分类日志
/// </summary>
/// <param name="info">日志信息</param>
/// <param name="dirName">保存目录名,形如d:\log\aaa</param>
private static void WriteSortLog(string info, string dirName)
{
try
{
if (false == System.IO.Directory.Exists(dirName))
{
System.IO.Directory.CreateDirectory(dirName);
}
string path = dirName + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss: ") + info);
sw.Close();
}
catch (Exception ex)
{
string expMsg = "WriteSortLog异常:" + ex.Message + Environment.NewLine + ex.StackTrace;
if (ex.InnerException != null)
expMsg += Environment.NewLine + "InnerException:" + ex.InnerException.Message;
Error(expMsg, ex);
}
}
/// <summary>
/// Info级 常规日志
/// </summary>
/// <param name="info">日志信息</param>
public static void Info(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
/// <summary>
/// Info 先生成常规日志,然后在指定目录另外创建一份日志
/// 主要用来需要对日志进行分类时使用
/// </summary>
/// <param name="info"></param>
/// <param name="dirName"></param>
public static void Info(string info, string dirName)
{
if (loginfo.IsInfoEnabled)
{
//生成常规日志
loginfo.Info(info);
//生成分类日志
WriteSortLog(info, dirName);
}
}
/// <summary>
/// Debug级 常规日志
/// </summary>
/// <param name="info">日志信息</param>
public static void Debug(string info)
{
if (loginfo.IsDebugEnabled)
{
loginfo.Debug(info);
}
}
/// <summary>
/// Debug级 异常日志
/// </summary>
/// <param name="info">日志信息</param>
/// <param name="exp">异常信息</param>
public static void Debug(string info, Exception exp)
{
if (loginfo.IsDebugEnabled)
{
loginfo.Debug(info, exp);
}
}
/// <summary>
/// Error级 常规的日志
/// </summary>
/// <param name="info">日志信息</param>
public static void Error(string info)
{
if (loginfo.IsErrorEnabled)
{
loginfo.Error(info);
}
}
/// <summary>
/// Error 异常日志
/// </summary>
/// <param name="info">日志信息</param>
/// <param name="exp">异常信息</param>
public static void Error(string info, Exception exp)
{
if (loginfo.IsErrorEnabled)
{
loginfo.Error(info, exp);
}
}
/// <summary>
/// Fatal级 常规日志
/// </summary>
/// <param name="info">日志信息</param>
public static void Fatal(string info)
{
if (loginfo.IsFatalEnabled)
{
loginfo.Fatal(info);
}
}
/// <summary>
/// Fatal级 异常日志
/// </summary>
/// <param name="info">日志信息</param>
/// <param name="exp">异常信息</param>
public static void Fatal(string info, Exception exp)
{
if (loginfo.IsFatalEnabled)
{
loginfo.Fatal(info, exp);
}
}
/// <summary>
/// Warn级 常规日志
/// </summary>
/// <param name="info">日志信息</param>
public static void Warn(string info)
{
if (loginfo.IsWarnEnabled)
{
loginfo.Warn(info);
}
}
/// <summary>
/// Warn级 异常日志
/// </summary>
/// <param name="info">日志</param>
/// <param name="exp">异常信息</param>
public static void Warn(string info, Exception exp)
{
if (loginfo.IsWarnEnabled)
{
loginfo.Warn(info, exp);
}
}
}
}
4.测试
调用 logHelp 类中的日志输出
LogHelp.SetConfig();
LogHelp.Warn("cs1");//不加可能没有日志输出,自测
LogHelp.Info("cs");
LogHelp.Debug("cs");
Console.ReadKey();