引用dll : log4net.dll
接口类:ILogger.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HCCD.Base.Comm.NewLogger
{
///
/// 日志接口
///
public interface ILogger
{
///
/// 调试日志输出
///
/// 输出内容
void Debug(string msg);
///
/// 调试日志输出
///
/// 输出内容
/// 输出异常
void Debug(string msg, Exception ex);
///
/// 信息日志输出
///
/// 输出内容
void Info(string msg);
///
/// 信息日志输出
///
/// 输出内容
/// 输出异常
void Info(string msg, Exception ex);
///
/// 警告日志输出
///
/// 输出内容
void Warn(string msg);
///
/// 警告日志输出
///
/// 输出内容
/// 输出异常
void Warn(string msg, Exception ex);
///
/// 错误日志输出
///
/// 输出内容
void Error(string msg);
///
/// 错误日志输出
///
/// 输出内容
/// 输出异常
void Error(string msg, Exception ex);
///
/// 致命日志输出
///
/// 输出内容
void Fatal(string msg);
///
/// 致命日志输出
///
/// 输出内容
/// 输出异常
void Fatal(string msg, Exception ex);
}
}
接口实现类:LogHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Appender;
using System.IO;
using log4net.Config;
namespace HCCD.Base.Comm.NewLogger
{
/*
* Author: Long
* Date: 2019-09
* Detail: Log4net日志类
*/
///
/// Log4net日志类
///
public class LogHelper : ILogger
{
private Dictionary LogDic = new Dictionary();
private object _islock = new object();
private string fileName = string.Empty;
///
/// 日志调用初始化
///
/// 日志文件保存路径[若路径为空,则默认程序根目录Logger文件夹;]
/// 日志文件名[若文件名为空,则默认文件名:Default]
public LogHelper(string fileSavePath, string fileName,string logSuffix = ".log")
{
try
{
Init();
if (string.IsNullOrEmpty(fileSavePath))
fileSavePath = "Logger";
if (string.IsNullOrEmpty(fileName))
fileName = "Default";
this.fileName = fileName;
var repository = LogManager.GetRepository();
var appenders = repository.GetAppenders();
if (appenders.Length == 0) return;
var targetApder = appenders.First(p => p.Name == "FileInfoAppender") as RollingFileAppender;
targetApder.File = Path.Combine(fileSavePath, this.fileName + logSuffix);
targetApder.ActivateOptions();
}
catch (Exception ex) { }
}
///
/// 缓存日志对象
///
///
///
private ILog GetLog(string name)
{
try
{
if (LogDic == null)
{
LogDic = new Dictionary();
}
lock (_islock)
{
if (!LogDic.ContainsKey(name))
{
LogDic.Add(name, LogManager.GetLogger(name));
}
}
return LogDic[name];
}
catch
{
return LogManager.GetLogger("Default");
}
}
///
/// 日志记录初始化
///
private void Init()
{
var file = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4net.config"));
XmlConfigurator.Configure(file);
}
///
/// 调试日志输出
///
/// 输出内容
public void Debug(string msg)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Debug(msg);
}
///
/// 调试日志输出
///
/// 输出内容
/// 输出异常
public void Debug(string msg, Exception ex)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Debug(msg, ex);
}
///
/// 信息日志输出
///
/// 输出内容
public void Info(string msg)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Info(msg);
}
///
/// 信息日志输出
///
/// 输出内容
/// 输出异常
public void Info(string msg, Exception ex)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Info(msg, ex);
}
///
/// 警告日志输出
///
/// 输出内容
public void Warn(string msg)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Warn(msg);
}
///
/// 警告日志输出
///
/// 输出内容
/// 输出异常
public void Warn(string msg, Exception ex)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Warn(msg, ex);
}
///
/// 错误日志输出
///
/// 输出内容
public void Error(string msg)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Error(msg);
}
///
/// 错误日志输出
///
/// 输出内容
/// 输出异常
public void Error(string msg, Exception ex)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Error(msg, ex);
}
///
/// 致命日志输出
///
/// 输出内容
public void Fatal(string msg)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Fatal(msg);
}
///
/// 致命日志输出
///
/// 输出内容
/// 输出异常
public void Fatal(string msg, Exception ex)
{
var log = GetLog(this.fileName);
if (log == null)
{
return;
}
log.Fatal(msg, ex);
}
}
}
配置类:Log4net.config
调用方式
引用:log4net.dll
HCC.Base.Comm.NewLogger.dll
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCC.Base.Comm.NewLogger;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
LogHelper log = new LogHelper(@"D:\Logger", "stydy");
log.Info("广东省个大三噶的首付打好分单发核辐射的");
log.Info("广东省个大三噶的首付打好分单发核辐射的", new Exception("请检查系统异常!这个问题是相当的严重。"));
log.Fatal("gdsgdsgdsgdsgdsgds");
log.Fatal("gdsgdsgdsgdsgdsgds", new Exception("请检查系统异常!这个问题是相当的严重。"));
log.Warn("广东省个大三噶电视柜电视柜");
log.Warn("gdsgdsgdsgdsgdsgds", new Exception("请检查系统异常!这个问题是相当的严重。"));
log.Debug("cbvcbvfgsfgdsgdsgdsgdsgdsgds");
log.Debug("gdsgdsgdsgdsgdsgds", new Exception("请检查系统异常!这个问题是相当的严重。"));
log.Error("系统异常");
log.Error("系统异常",new Exception("请检查系统异常!这个问题是相当的严重。"));
}
}
}