1、先项目中包管理 安装
2、在.net core 项目中Program.cs文件增加
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) => {
logging.ClearProviders();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseNLog()
.UseWindowsService();
3、全局的使用
private static Logger logger = LogManager.GetCurrentClassLogger();
4、多租户
private static NLog.ILogger CreateNLogInstance(string basePath, string TenancyFID, string repositoryName)
{
NLog.ILogger logTemp;
var path = SystemParas.AppBasePath + "\\MrNlog.config";
NLog.LogFactory logFactory = new NLog.LogFactory();
//动态构建NLog配置文件
//logFactory.Configuration = CreateLoggingConfiguration(basePath, repositoryName);
//从配置文件读取NLog配置文件 修改其保存得路径和归档文件
logFactory.Configuration = new NLog.Config.XmlLoggingConfiguration(path, true, logFactory);
var fTagert = logFactory.Configuration.FindTargetByName<FileTarget>(TargetName);
fTagert.FileName = basePath + "/Log/Mes-Server-Log ${date:format=yyyy-MM-dd-HH}.log";//${shortdate}/${level}
if (fTagert.ArchiveFileName != null)
{
fTagert.ArchiveFileName = basePath + "/Log/archives/log.{#####}.txt";//归档文件名
fTagert.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
}
//NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel);
var rule = logFactory.Configuration.LoggingRules.Where(x => x.Targets.Any(s => s.Name == TargetName)).FirstOrDefault();
logTemp = logFactory.GetCurrentClassLogger();
return logTemp;
}
5、动态修改日志等级
static private void SetNLogFactoryParams(LogFactory logFactory, int LogFileMaxSize)
{
var fileNotTarget = logFactory.Configuration.FindTargetByName<FileTarget>(TargetName);
if (fileNotTarget == null)
{
return;
}
//fileNotTarget.MaxArchiveDays = baseLogFileMaxNumber;
//fileNotTarget.MaxArchiveFiles = LogFileMaxSize;
var ruleNot = logFactory.Configuration.LoggingRules.Where(x => x.Targets.Any(s => s.Name == TargetName)).FirstOrDefault();
if (ruleNot == null)
{
return;
}
if (logLevelEnm == LogLevelEnum.ALL)
{
ruleNot.EnableLoggingForLevel(LogLevel.Debug);
ruleNot.EnableLoggingForLevel(LogLevel.Trace);
ruleNot.EnableLoggingForLevel(LogLevel.Info);
ruleNot.EnableLoggingForLevel(LogLevel.Warn);
ruleNot.EnableLoggingForLevel(LogLevel.Error);
ruleNot.EnableLoggingForLevel(LogLevel.Fatal);
}
else if (logLevelEnm == LogLevelEnum.INFO)
{
ruleNot.EnableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Warn);
ruleNot.DisableLoggingForLevel(LogLevel.Error);
ruleNot.DisableLoggingForLevel(LogLevel.Fatal);
}
else if (logLevelEnm == LogLevelEnum.WARN)
{
ruleNot.EnableLoggingForLevel(LogLevel.Warn);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Error);
ruleNot.DisableLoggingForLevel(LogLevel.Fatal);
}
else if (logLevelEnm == LogLevelEnum.DEBUG)
{
ruleNot.EnableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Error);
ruleNot.DisableLoggingForLevel(LogLevel.Fatal);
}
else if (logLevelEnm == LogLevelEnum.ERROR)
{
ruleNot.EnableLoggingForLevel(LogLevel.Error);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Fatal);
}
else if (logLevelEnm == LogLevelEnum.FATAL)
{
ruleNot.EnableLoggingForLevel(LogLevel.Fatal);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Error);
}
else if (logLevelEnm == LogLevelEnum.OFF)
{
ruleNot.EnableLoggingForLevel(LogLevel.Off);
ruleNot.DisableLoggingForLevel(LogLevel.Fatal);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Trace);
ruleNot.DisableLoggingForLevel(LogLevel.Info);
ruleNot.DisableLoggingForLevel(LogLevel.Debug);
ruleNot.DisableLoggingForLevel(LogLevel.Error);
}
// 只有加这个才会起效果
logger.nLogger.Factory.ReconfigExistingLoggers();
}
6、配置文件 MrNlog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace" internalLogFile="D:\temp\nlog-internal.log">
<targets>
<target xsi:type="File" name="f" fileName="D:/logs/Nlog/${shortdate}/nlog.log"
layout="${newline} ${longdate} ${uppercase:${level}} ${threadid} ${newline} Message: ${message} ${newline}}" encoding="utf-8"
archiveEvery="Hour" archiveAboveSize="2097152"
maxArchiveDays="1"
maxArchiveFiles="2"/>
</targets>
<rules>
<!--archiveFileName="${LogHome}/Archive/${shortdate}.txt"-->
<!--Skip Microsoft logs and so log only own logs-->
<!--以Microsoft打头的日志将进入此路由,由于此路由没有writeTo属性,所有会被忽略${newline}${onexception:Exception:${exception:format=toString}-->
<!--且此路由设置了final,所以此路由被匹配到了不会再继续往下匹配。未匹配到的会继续匹配下一个路由-->
<!--<logger name="Microsoft.*" minlevel="Trace" final="true" />-->
<logger name="*" level="Off" writeTo="f" />
</rules>
</nlog>