|
1、安装 NLog.Web.AspNetCore 包
2、新增Configs文件夹,在Configs文件夹下新增nlog.config文件
nlog.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">
- <targets>
-
- <target name="InfoFile" xsi:type="AsyncWrapper">
-
- <target name="log_file" xsi:type="File"
- fileName="${basedir}/Logs/${shortdate}/${shortdate}-InfoFile.txt"
- layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
- archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
- archiveAboveSize="102400"
- archiveNumbering="Sequence"
- concurrentWrites="true"
- keepFileOpen="false" />
- </target>
- <target name="WarnFile" xsi:type="AsyncWrapper">
-
- <target name="log_file" xsi:type="File"
- fileName="${basedir}/Logs/${shortdate}/${shortdate}-WarnFile.txt"
- layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
- archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
- archiveAboveSize="102400"
- archiveNumbering="Sequence"
- concurrentWrites="true"
- keepFileOpen="false" />
- </target>
-
-
- <target name="colorConsole" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
- </targets>
-
- <rules>
- <logger name="Microsoft.*" minlevel="Info" writeTo="" final="true" />
- <logger name="*" minlevel="Info" writeTo="InfoFile" />
- <logger name="*" minlevel="Warn" writeTo="WarnFile" />
-
- </rules>
复制代码
3、Program.cs 中 注入- // NLog:注册Nlog组件
- builder.Logging.AddNLog("Configs/nlog.config");
复制代码 4、新增帮助类LoggerHelper- /// <summary>
- /// nLog使用帮助类
- /// </summary>
- public class LoggerHelper
- {
- /// <summary>
- /// 实例化nLog,即为获取配置文件相关信息(获取以当前正在初始化的类命名的记录器)
- /// </summary>
- private readonly static NLog.Logger _logger = LogManager.GetCurrentClassLogger();
- private static LoggerHelper _obj;
- public static LoggerHelper _
- {
- get => _obj ?? (new LoggerHelper());
- set => _obj = value;
- }
- #region Debug,调试
- public static void Debug(string msg)
- {
- _logger.Debug(msg);
- }
- public void Debug(string msg, Exception err)
- {
- _logger.Debug(err, msg);
- }
- #endregion
- #region Info,信息
- public static void Info(string msg)
- {
- _logger.Info(msg);
- }
- public void Info(string msg, Exception err)
- {
- _logger.Info(err, msg);
- }
- #endregion
- #region Warn,警告
- public static void Warn(string msg)
- {
- _logger.Warn(msg);
- }
- public void Warn(string msg, Exception err)
- {
- _logger.Warn(err, msg);
- }
- #endregion
- #region Trace,追踪
- public void Trace(string msg)
- {
- _logger.Trace(msg);
- }
- public void Trace(string msg, Exception err)
- {
- _logger.Trace(err, msg);
- }
- #endregion
- #region Error,错误
- public void Error(string msg)
- {
- _logger.Error(msg);
- }
- public void Error(string msg, Exception err)
- {
- _logger.Error(err, msg);
- }
- #endregion
- #region Fatal,致命错误
- public void Fatal(string msg)
- {
- _logger.Fatal(msg);
- }
- public void Fatal(string msg, Exception err)
- {
- _logger.Fatal(err, msg);
- }
- #endregion
- }
复制代码 5、调用
在接口或者需要写入日志的地方- LoggerHelper.Info("TEST111111111111111111111111111111111111111111111");
- LoggerHelper.Warn("TEST222222222222222222222222222222222222222222");
复制代码
说明:本内容仅仅作为学习记录,无任何商业目的。
来源:https://www.cnblogs.com/hkzw/archive/2023/03/15/17217251.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|