NLog是适用于各种.NET平台的灵活,免费的日志记录平台。NLog使写入多个目标变得容易 。(数据库,文件,控制台)并即时更改日志记录配置。(本文主要介绍写日志到文件中)
Nlog官网:https://nlog-project.org/
Nlog配置说明:https://nlog-project.org/config/
Nlog Github Wiki:https://github.com/NLog/NLog/wiki
注:以下操作为 asp.net core web项目,编辑器为VS2019
PM> Install-Package NLog
dotnet add package NLog
在项目的根目录下,在cmd窗口中输入上面的命令即可安装。
下面的配置,就是一个简单的配置Nlog.config的demo,将下面的配置复制到Nlog.config中,即可将日志写入到PsTest.log文件中。
<?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">
<targets>
<target name="file" xsi:type="File"
fileName="${basedir}\PsTest.log"
layout="${longdate} | ${level:uppercase=true}: ${message} | ${stacktrace}"
maxArchiveFiles="3"
archiveAboveSize="10240"
encoding="utf-8"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"></logger>
</rules>
</nlog>
下面简单介绍下各配置项的意思:
targets 节点中定义了一系列日志输出目标,每一个输出目标是一个 target 元素。
target标签:
rules 节点是日志路由规则的集合,由一个或多个 logger 元素组成。每个 logger 元素记录了 logger 的名字、目标输出以及要处理的日志等级。
logger标签:
public static void Main(string[] args)
{
var i = 100;
while (i > 1)
{
NlogTest();
i--;
}
}
public static void NlogTest()
{
//通过NLog.LogManager.GetCurrentClassLogger方法可以创建一个与所在类同名(包括 namespace)的 NLog.Logger 的实例。
var logger = LogManager.GetCurrentClassLogger();
logger.Error("something Error!");
logger.Info("something Info!");
logger.Debug("something Debug!");
try
{
List<int> ls = null;
var c = ls.Count;
}
catch (Exception ex)
{
logger.Error(ex.Message);
}
}
来到应用的生成目录下面即可看到PsTest.log日志文件。
可以看到生成了多个log文件,因为配置文件设置的log文件最大为10k。
打开PsTest.0.log文件:
日志已经按照配置的格式写入到文件中!
但这样写有个问题,就是每次写日志都要先创建一个NLog.Logger 的实例,这样的话,如果程序集较多,就造成了代码冗余,所以,还是封装一个Nlog的帮助类比较好,每次都调用这个类去写日志
public class PsLog
{
public static void Error(Exception ex)
{
Exception iex = GetInnerException(ex);
Error(iex.ToString());
}
public static void Error(string msg)
{
WriteLog(msg, LogLevel.Error);
}
private static void WriteLog(string msg, LogLevel level)
{
try
{
if (LogLevel.Debug == level)
{
LogManager.GetCurrentClassLogger().Debug(msg);
}
else if (LogLevel.Error == level)
{
LogManager.GetCurrentClassLogger().Error(msg);
}
else if (LogLevel.Fatal == level)
{
LogManager.GetCurrentClassLogger().Fatal(msg);
}
else if (LogLevel.Info == level)
{
LogManager.GetCurrentClassLogger().Info(msg);
}
else if (LogLevel.Trace == level)
{
LogManager.GetCurrentClassLogger().Trace(msg);
}
else if (LogLevel.Warn == level)
{
LogManager.GetCurrentClassLogger().Warn(msg);
}
else
{
LogManager.GetCurrentClassLogger().Info(msg);
}
}
catch (System.Exception e)
{
try
{
string strPath = Directory.GetCurrentDirectory();
using (FileStream fs = File.Open(strPath + @"\fatal.txt", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
sw.WriteLine(DateTime.Now.ToString() + ": Can not write the nlog, " + e.Message);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public static void Info(string msg)
{
WriteLog(msg, LogLevel.Info);
}
private static Exception GetInnerException(Exception ex)
{
if (ex.InnerException == null)
{
return ex;
}
return GetInnerException(ex.InnerException);
}
}
public static void NlogTest()
{
PsLog.Error("something Error!");
PsLog.Info("something Info!");
PsLog.Debug("something Debug!");
try
{
List<int> ls = null;
var c = ls.Count;
}
catch (Exception ex)
{
PsLog.Error(ex);
}
}
原文:https://www.cnblogs.com/willingtolove/p/12129828.html