nlog

Nlog日志框架,支持.Net与.Net Core

 
一.在Asp Net Mvc中使用步骤
1.Nuget安装包 NLog与NLog.Config
2.安装完成后,目录下会生成Nlog.config与Nlog.xsd文件
3.新建一个Config文件夹,将两个文件放到此文件夹下,如下图
4.在web.config中配置如下节点

<configuration>
   <configSections>
     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
   </configSections>
   <nlog>
     <include file="${basedir}/Config/NLog.config" /><!--包含文件 此处配置为前面引用NLog时添加的NLog.config文件-->
   </nlog>
 </configuration>
 
5.将Nlog.config设置为 如果较新则复制 或 始终复制
 
6.封装NLog
class Logger
    {
        NLog.Logger logger;

        private Logger(NLog.Logger logger)
        {
            this.logger = logger;
        }

        public Logger(string name)
            :this(NLog.LogManager.GetLogger(name))
        {
        }

        public static Logger Default { get; private set; }
        static Logger()
        {
            Default = new Logger(NLog.LogManager.GetCurrentClassLogger());
        }

        public void Debug(string msg, params object[] args)
        {
            logger.Debug(msg, args);
        }

        public void Debug(string msg, Exception err)
        {
            logger.Debug(msg, err);
        }

        public void Info(string msg, params object[] args)
        {
            logger.Info(msg, args);
        }

        public void Info(string msg, Exception err)
        {
            logger.Info(msg, err);
        }

        public void Trace(string msg, params object[] args)
        {
            logger.Trace(msg, args);
        }

        public void Trace(string msg, Exception err)
        {
            logger.Trace(msg, err);
        }

        public void Error(string msg, params object[] args)
        {
            logger.Error(msg, args);
        }

        public void Error(string msg, Exception err)
        {
            logger.Error(msg, err);
        }

        public void Fatal(string msg, params object[] args)
        {
            logger.Fatal(msg, args);
        }

        public void Fatal(string msg, Exception err)
        {
            logger.Fatal(msg, err);
        }
    }
 
7.使用方法

Logger.Default.Debug("出现异常了!",e)
 
8.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="console" xsi:type="ColoredConsole"
                        layout="${date:format=HH\:mm\:ss}> ${message}"/>

		<!--VS输出窗口-->
		<target name="debugger" xsi:type="Debugger"
                        layout="${date:format=HH\:mm\:ss} | ${level:padding=-5} | ${message}" />

		<!--保存至文件-->
		<target name="error_file" xsi:type="File" encoding="utf-8" maxArchiveFiles="10"
                        fileName="${basedir}/Logs/${shortdate}_log.txt"
                        layout="${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
	</targets>
	<rules>
		<!--<logger name="*" writeTo="console" />-->
		<logger name="*" minlevel="Debug" writeTo="debugger" />
		<logger name="*" minlevel="Info" writeTo="error_file" />
	</rules>
</nlog>
 
二.在Asp Net Core Mvc/Webapi 中使用步骤
1.在web运行目录中添加Nuget包引用 NLog.Web.AspNetCore
 
 
2.在Program.cs文件Main方法中添加配置

NLog.Web.NLogBuilder.ConfigureNLog("Config/nlog.config");   //主意:这里的路径是nlog.config的路径
 
3.使用方法【借助上面 封装NLog】
Logger.Default.Debug("出现异常了!",e)

posted @ 2021-11-20 22:39  ssnice  阅读(93)  评论(0编辑  收藏  举报