这个类就是对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);
}

结束。

c#一个日志类(log4net)的更多相关文章

  1. C++ IO操作API及注意事项(包含一个日志类的实现)

    C++是一个抽象程度比C高很多的语言,在使用C++时,编译器做了很多工作,如果我们不对C++的某些特性的实现机制进行了解,那么编程时也许会有很多疑惑,我们也许知道怎样做才是正确的,但不知道为什么要这样 ...

  2. [C#] 日志类

    在程序发布到服务器上的时候,不能在像本地执行一样可以调试,在发生错误时候,往往不能很方便的查找错误.将错误信息写入文件是一种比较常用的处理方法.以下是一个日志类,实现以下功能: 1)按日期每天生产不同 ...

  3. 一个不需要Log4Net的写日志的简单方法

    有些项目写日志时会选择大名鼎鼎的Log4Net.而在我们使用它时,总会出现一些诸如版本不匹配而造成的写日志失败的情况,还要改web.config,还要改AssemblyInfo.而且,它的失败,并不是 ...

  4. 20181015记录一个简单的TXT日志类

    20190422添加换行以及时间记录 using System; using System.Collections.Generic; using System.IO; using System.Lin ...

  5. 搭建一套自己实用的.net架构(2)【日志模块-log4net】

    先谈谈简单的模块,日志.在系统中日志模块是必须的,什么系统日志,操作日志,调试日志.这里用的是log4net. 对log4net还不熟悉的小伙伴们赶快去搜索基础教程哦, 我这里就不温故了. 那么有人要 ...

  6. webapi框架搭建-日志管理log4net

    前言 本篇讲怎么在前几篇已经创建好的项目里加上日志处理机制,我们采用Log4net技术.跟多的log4net技术的细节请查阅log4net的官网. log4net官网:http://logging.a ...

  7. 基于log4net的日志组件扩展封装,实现自动记录交互日志 XYH.Log4Net.Extend(微服务监控)

    背景: 随着公司的项目不断的完善,功能越来越复杂,服务也越来越多(微服务),公司迫切需要对整个系统的每一个程序的运行情况进行监控,并且能够实现对自动记录不同服务间的程序调用的交互日志,以及通一个服务或 ...

  8. PHP用单例模式实现一个数据库类

    使用单例模式的出发点: 1.php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作, 使用单例模式, 则可以避免大量的new 操作消耗的资源. 2.如果系统中需要有一个类来全局控制某些 ...

  9. 使用libzplay库封装一个音频类

    装载请说明原地址,谢谢~~      前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...

随机推荐

  1. Jupyter 常用快捷键 及 常用方法笔记

      两个不同的cell有上下的关系, 不是完全独立的, 下图可以看出下面的res是引用上面的   保存节点        就像虚拟机的快照与恢复 回到节点 保存文件 s(快捷键)        实际写 ...

  2. 转:hive面试题

    有一张很大的表:TRLOG该表大概有2T左右TRLOG:CREATE TABLE TRLOG(PLATFORM string,USER_ID int,CLICK_TIME string,CLICK_U ...

  3. Android sqlite日期存储

    SQLite日期类型是以TEXT.REAL和INTEGER类型分别不同的格式表示的,对应如下:TEXT: "YYYY-MM-DD HH:MM:SS.SSS"REAL: 以Julia ...

  4. idea 使用 git打成jar包到 nexus

    1.使用idea生成jar包参考:http://blog.csdn.net/eastgrand/article/details/11945309 2.进入到 自己的工程目录(含有pom.xml的目录) ...

  5. c# 依赖注入之---反射(转)

    详细请看http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 定义一个接口,和两个类(实现该接口) IButton: using ...

  6. SQL Server ->> Sparse File(稀疏文件)

    Sparse File(稀疏文件)不是SQL Server的特性.它属于Windows的NTFS文件系统的一个特性.如果某个大文件中的数据包含着大量“0数据”(这个应该从二进制上看),这样的文件就可以 ...

  7. jbd2/dm-2-8 io太高

    用iotop查看发现[jbd2/dm-2-8]几乎占用了99%的io使用率,但是却没有输入输出 后来上网查找,网上有人说是个内核bug,需要升级内核,或者降低jdb2的提交次数,即重新挂载磁盘 添加c ...

  8. js笔记 标签: javascript 2016-08-01 13:30 75人阅读 评论(0) 收藏

    typeof可以用来检测给定变量的数据类型,typeof是一个操作符而不是函数,所以圆括号可以省略. Undefined类型只有一个值,即特殊的undefined.在使用var声明变量但未对其加以初始 ...

  9. python .loc vs .iloc区别

    1.loc意义:通过行标签索引行数据 例: loc[n]表示索引的是第n行(index 是整数) loc[‘d’]表示索引的是第’d’行(index 是字符) 2. .iloc   :通过行号获取行数 ...

  10. OC NSMutableArray

    #import <Foundation/Foundation.h> #import "Student.h" void arrayCreate() { NSMutable ...