asp.net core 3.1 使用log4net
标签:net 调用 单例模式 repos public dir 名称 err 文件属性
1.引用Log4net到项目中,搜NuGet包 Log4net 现在最新是 Version 2.0.8
2.在项目中添加log4net.config文件,右键改文件属性-》复制到输出目录选择-》始终复制
"1.0" encoding="utf-8" ?>
"DebugAppender" type="log4net.Appender.DebugAppender" >
"log4net.Layout.PatternLayout">
"%date [%thread] %-5level %logger - %message%newline" />
"RollingFile" type="log4net.Appender.RollingFileAppender">
"Log/" />
"true" />
"MaxSizeRollBackups" value= "10"/>
"maximumFileSize" value="1MB" />
"RollingStyle" value="Date" />
"DatePattern" value=""logs_"yyyyMMdd".log"" />
"StaticLogFileName" value="false" />
"log4net.Layout.PatternLayout">
"%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
"ALL"/>
ref ref="DebugAppender" />
ref ref="RollingFile" />
3.编写Log4netHelper
using log4net;
using log4net.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace NF.Common.Utility
{
///
/// 日志等级
///
public enum LogLevel
{
Error,
Debug,
Warning,
Info
}
///
/// 单例模式初始化
///
public class Singleton
{
private ILog Log;
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
///
/// 获取日志初始化器
///
/// 类名 方法名
///
public ILog Init(string type)
{
Log = LogManager.GetLogger(Log4netHelper.Repository.Name, type);
return Log;
}
}
///
/// 日志操作类
///
public class Log4netHelper
{
///
/// log4net 仓储
///
public static ILoggerRepository Repository { get; set; }
///
/// 输出Erro日志
///
/// 日志内容
public static void Error(string message)
{
StackTrace trace = new StackTrace();
//获取是哪个类来调用的
var className = trace.GetFrame(1).GetMethod().DeclaringType;
//获取方法名称
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name;
WriteLog(LogLevel.Error, message, type);
}
///
/// 输出Warning日志
///
/// 日志内容
public static void Warning(string message)
{
StackTrace trace = new StackTrace();
//获取是哪个类来调用的
var className = trace.GetFrame(1).GetMethod().DeclaringType;
//获取方法名称
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name;
//记录日志
WriteLog(LogLevel.Warning, message, type);
}
///
/// 输出Info日志
///
/// 日志内容
public static void Info(string message)
{
StackTrace trace = new StackTrace();
//获取是哪个类来调用的
var className = trace.GetFrame(1).GetMethod().DeclaringType;
//获取方法名称
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name;
//记录日志
WriteLog(LogLevel.Info, message, type);
}
///
/// 输出Debug日志
///
/// 日志内容
public static void Debug(string message)
{
StackTrace trace = new StackTrace();
//获取是哪个类来调用的
var className = trace.GetFrame(1).GetMethod().DeclaringType;
//获取方法名称
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name;
//记录日志
WriteLog(LogLevel.Debug, message, type);
}
///
/// 写日志
///
/// 日志等级
/// 日志信息
/// 类名 方法名
private static void WriteLog(LogLevel logLevel, string message, string type)
{
ILog Log = Singleton.getInstance().Init(type);
switch (logLevel)
{
case LogLevel.Debug:
Log.Debug(message);
break;
case LogLevel.Error:
Log.Error(message);
break;
case LogLevel.Info:
Log.Info(message);
break;
case LogLevel.Warning:
Log.Warn(message);
break;
}
}
}
}
4、修改Startup 在ConfigureServices(IServiceCollection services)方法里加入如下代码:
Log4netHelper.Repository = LogManager.CreateRepository("NETCoreRepository");
XmlConfigurator.Configure(Log4netHelper.Repository, new FileInfo(Environment.CurrentDirectory + "/Config/log4net.config"));
asp.net core 3.1 使用log4net
标签:net 调用 单例模式 repos public dir 名称 err 文件属性
原文地址:https://www.cnblogs.com/linyijia/p/12986574.html
评论