log4net日志配置
//log4net日志
public static ILoggerRepository repository { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
repository = LogManager.CreateRepository("AprilLog");
XmlConfigurator.Configure(repository, new FileInfo("Config/log4net.config"));//配置文件路径可以自定义
BasicConfigurator.Configure(repository);
}
log4net.config文件配置
logUtil.cs类
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xiaowu.Common
{
///
/// 日志类
///
public class LogUtil
{
//https://www.cnblogs.com/AprilBlank/p/11282345.html
private static readonly ILog log = LogManager.GetLogger("AprilLog", typeof(LogUtil));
///
/// 调试日志
///
///
///
public static void Debug(string msg, object obj = null)
{
if (log.IsDebugEnabled && !string.IsNullOrEmpty(msg))
{
if (obj == null)
{
log.Debug(msg);
}
else
{
log.DebugFormat(msg, obj);
}
}
}
///
/// 日常日志
///
///
///
public static void Info(string msg, object obj = null)
{
if (log.IsInfoEnabled && !string.IsNullOrEmpty(msg))
{
if (obj == null)
{
log.Info(msg);
}
else
{
log.InfoFormat(msg, obj);
}
}
}
///
/// 错误日志
///
///
///
public static void Error(string msg, object obj = null)
{
if (log.IsErrorEnabled && !string.IsNullOrEmpty(msg))
{
if (obj == null)
{
log.Error(msg);
}
else
{
log.ErrorFormat(msg, obj);
}
}
}
///
/// 重要日志
///
///
///
public static void Fatal(string msg, object obj = null)
{
if (log.IsFatalEnabled && !string.IsNullOrEmpty(msg))
{
if (obj == null)
{
log.Fatal(msg);
}
else
{
log.FatalFormat(msg, obj);
}
}
}
}
}
原文:https://www.cnblogs.com/wugh8726254/p/15000226.html