首页 技术 正文
技术 2022年11月12日
0 收藏 407 点赞 3,312 浏览 5378 个字

这个类就是对log4net的使用,就不多说了,但是看见网上的一个封装,自己用了下,感觉还不错,直接记录在这里。把自己使用的类直接贴出来。

using log4net;
using log4net.Config;
using System;
using System.Reflection;
/**
* 命名空间: Hikari
* 类 名: Logger
* CLR版本: 4.0.30319.42000
* 版本 :v1.0
* Copyright (c)
*/[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace Hikari
{
/// <summary>
/// 功能描述 :Logger
/// 创 建 者 :
/// 创建日期 :2018/10/26 20:44:15
/// 最后修改者 :
/// 最后修改日期:2018/10/26 20:44:15
/// </summary> public sealed class Logger
{
#region [ 单例模式 ] private static Logger logger;
private static readonly log4net.ILog _Logger4net = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); /// <summary>
/// 无参私有构造函数
/// </summary>
private Logger()
{
} /// <summary>
/// 得到单例
/// </summary>
public static Logger Singleton
{
get
{
if (logger == null)
{
logger = new Logger();
}
return logger;
}
} /// <summary>
/// 设置配置
/// </summary>
/// <param name="path"></param>
public void LogConfiguration(string path)
{
if(string.IsNullOrEmpty(path))
{
return;
}
try
{
XmlConfigurator.ConfigureAndWatch(null, new System.IO.FileInfo(path));
}
catch(Exception ex)
{
Console.WriteLine("Error:配置日志错误," + ex.Message);
}
}
#endregion #region [ 参数 ] public bool IsDebugEnabled
{
get { return _Logger4net.IsDebugEnabled; }
}
public bool IsInfoEnabled
{
get { return _Logger4net.IsInfoEnabled; }
}
public bool IsWarnEnabled
{
get { return _Logger4net.IsWarnEnabled; }
}
public bool IsErrorEnabled
{
get { return _Logger4net.IsErrorEnabled; }
}
public bool IsFatalEnabled
{
get { return _Logger4net.IsFatalEnabled; }
} #endregion #region [ 接口方法 ] #region [ Debug ] public void Debug(string message)
{
if (this.IsDebugEnabled)
{
this.Log(LogLevel.Debug, message);
}
} public void Debug(string message, Exception exception)
{
if (this.IsDebugEnabled)
{
this.Log(LogLevel.Debug, message, exception);
}
} public void DebugFormat(string format, params object[] args)
{
if (this.IsDebugEnabled)
{
this.Log(LogLevel.Debug, format, args);
}
} public void DebugFormat(string format, Exception exception, params object[] args)
{
if (this.IsDebugEnabled)
{
this.Log(LogLevel.Debug, string.Format(format, args), exception);
}
} #endregion #region [ Info ] public void Info(string message)
{
if (this.IsInfoEnabled)
{
this.Log(LogLevel.Info, message);
}
} public void Info(string message, Exception exception)
{
if (this.IsInfoEnabled)
{
this.Log(LogLevel.Info, message, exception);
}
} public void InfoFormat(string format, params object[] args)
{
if (this.IsInfoEnabled)
{
this.Log(LogLevel.Info, format, args);
}
} public void InfoFormat(string format, Exception exception, params object[] args)
{
if (this.IsInfoEnabled)
{
this.Log(LogLevel.Info, string.Format(format, args), exception);
}
} #endregion #region [ Warn ] public void Warn(string message)
{
if (this.IsWarnEnabled)
{
this.Log(LogLevel.Warn, message);
}
} public void Warn(string message, Exception exception)
{
if (this.IsWarnEnabled)
{
this.Log(LogLevel.Warn, message, exception);
}
} public void WarnFormat(string format, params object[] args)
{
if (this.IsWarnEnabled)
{
this.Log(LogLevel.Warn, format, args);
}
} public void WarnFormat(string format, Exception exception, params object[] args)
{
if (this.IsWarnEnabled)
{
this.Log(LogLevel.Warn, string.Format(format, args), exception);
}
} #endregion #region [ Error ] public void Error(string message)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, message);
}
} public void Error(string message, Exception exception)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, message, exception);
}
} public void ErrorFormat(string format, params object[] args)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, format, args);
}
} public void ErrorFormat(string format, Exception exception, params object[] args)
{
if (this.IsErrorEnabled)
{
this.Log(LogLevel.Error, string.Format(format, args), exception);
}
}
#endregion #region [ Fatal ] public void Fatal(string message)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, message);
}
} public void Fatal(string message, Exception exception)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, message, exception);
}
} public void FatalFormat(string format, params object[] args)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, format, args);
}
} public void FatalFormat(string format, Exception exception, params object[] args)
{
if (this.IsFatalEnabled)
{
this.Log(LogLevel.Fatal, string.Format(format, args), exception);
}
}
#endregion #endregion #region [ 内部方法 ]
/// <summary>
/// 输出普通日志
/// </summary>
/// <param name="level"></param>
/// <param name="format"></param>
/// <param name="args"></param>
private void Log(LogLevel level, string format, params object[] args)
{
switch (level)
{
case LogLevel.Debug:
_Logger4net.DebugFormat(format, args);
break;
case LogLevel.Info:
_Logger4net.InfoFormat(format, args);
break;
case LogLevel.Warn:
_Logger4net.WarnFormat(format, args);
break;
case LogLevel.Error:
_Logger4net.ErrorFormat(format, args);
break;
case LogLevel.Fatal:
_Logger4net.FatalFormat(format, args);
break;
}
} /// <summary>
/// 格式化输出异常信息
/// </summary>
/// <param name="level"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
private void Log(LogLevel level, string message, Exception exception)
{
switch (level)
{
case LogLevel.Debug:
_Logger4net.Debug(message, exception);
break;
case LogLevel.Info:
_Logger4net.Info(message, exception);
break;
case LogLevel.Warn:
_Logger4net.Warn(message, exception);
break;
case LogLevel.Error:
_Logger4net.Error(message, exception);
break;
case LogLevel.Fatal:
_Logger4net.Fatal(message, exception);
break;
}
} #endregion
}}

另外还有一个枚举

 public enum LogLevel
{
Debug,
Info,
Warn,
Error,
Fatal
}

然后就根据自己的需要使用单例记录日志。

当前类已经制定了加载日志配置文件。但是如果外部需要统一或者另外设置。则调用LogConfiguration方法传入文件。如果是另外的类配置,建议如此做:

 /// <summary>
/// 日志配置文件
/// </summary>
public string LogConfig { get { return logConfig; } set { logConfig = value; LogConfiguration(); } }还有个方法
/// <summary>
/// 配置日志
/// </summary>
private void LogConfiguration()
{
Logger.Singleton.LogConfiguration(logConfig);
}

结束。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845