如何利用log4Net自定义属性配置功能记录完整的日志信息

2025-04-03 21:20:49
推荐回答(1个)
回答1:

如何利用log4Net自定义属性配置功能记录完整的日志信息
log4Net作为专业的log记录控件,对于它的强大功能大家一定不陌生。下面我将详细介绍如何利用其自定义属性,让日志信息更完整。
一,创建测试工程,log4Net组件可以自己从网上下载,也可通过Nuget进行安装。
二,创建日志模型及数据库表,因为我们的日志信息可以输出为文本,也可以输出到数据库。
三,添加MyLayout,MyPatternConverter类扩展PatternLayout。
四,添加Log4Net.config文件,进行输入方式定义。





































































































五,添加LogHelper.cs类进行各自信息的写入操作。
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Log4NetTest
{
public class LogHelper
{
///
/// LoggerName
///

public static string LoggerName = string.Empty;
///
/// 用户ID
///

public static string UserID = string.Empty;
///
/// 用户名称
///

public static string UserName = string.Empty;
private static ILog iLog;
private static LogEntity logEntity;
///
/// 接口
///

private static ILog log
{
get
{
string path = Application.StartupPath + @"\Log4Net.config";
log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
if (iLog == null)
{
iLog = log4net.LogManager.GetLogger(LoggerName);
}
else
{
if (iLog.Logger.Name != LoggerName)
{
iLog = log4net.LogManager.GetLogger(LoggerName);
}
}
return iLog;
}
}
///
/// 构造消息实体
///

///
///
private static LogEntity BuildMessageMode(string message)
{
if (logEntity == null)
{
logEntity = new LogEntity();
logEntity.UserID = UserID;
logEntity.UserName = UserName;
logEntity.Message = message;
}
else
logEntity.Message = message;
return logEntity;
}
///
/// 调试
///

/// 消息
public static void Debug(string message)
{
if (log.IsDebugEnabled)
log.Debug(BuildMessageMode(message));
}
///
/// 调试
///

/// 消息
/// 异常
public static void Debug(string message, Exception ex)
{
if (log.IsDebugEnabled)
log.Debug(BuildMessageMode(message), ex);
}
///
/// 信息
///

/// 消息
public static void Info(string message)
{
if (log.IsInfoEnabled)
log.Info(BuildMessageMode(message));
}
///
/// 信息
///

/// 消息
/// 异常
public static void Info(string message, Exception ex)
{
if (log.IsInfoEnabled)
log.Info(BuildMessageMode(message), ex);
}
///
/// 一般错误
///

/// 消息
public static void Error(string message)
{
if (log.IsErrorEnabled)
log.Error(BuildMessageMode(message));
}
///
/// 一般错误
///

/// 消息
/// 异常
public static void Error(string message, Exception exception)
{
if (log.IsErrorEnabled)
log.Error(BuildMessageMode(message), exception);
}
///
/// 警告
///

/// 消息
public static void Warn(string message)
{
if (log.IsWarnEnabled)
log.Warn(BuildMessageMode(message));
}
///
/// 警告
///

/// 消息
/// 异常
public static void Warn(string message, Exception ex)
{
if (log.IsWarnEnabled)
log.Warn(BuildMessageMode(message), ex);
}
///
/// 严重
///

/// 消息
public static void Fatal(string message)
{
if (log.IsFatalEnabled)
log.Fatal(BuildMessageMode(message));
}
///
/// 严重
///

/// 消息
/// 异常
public static void Fatal(string message, Exception ex)
{
if (log.IsFatalEnabled)
log.Fatal(BuildMessageMode(message), ex);
}
}
}
六,进行日志效果测试,只要通过修改Log4Net.config,就可实现各种方式的输入。
输出到控制台:




输出到文件:




输出到数据库: