一、说明

  1. NLog介绍和使用说明官网:http://nlog-project.org/
  2. NLog和Log4net对比:https://www.cnblogs.com/qinjin/p/5134982.html

二、NLog集成步骤

  1. 下载模板项目,下载地址:https://aspnetboilerplate.com/Templates 选择.Net Core项目
  2. 新建一个.NET Standard类库项目Abp.Castle.NLog
  3. 添加NuGet包Castle.Core, Castle.LoggingFacility, NLog

  4. 参考abp log4net(ABP源码)添加class NLogLogger继承MarshalByRefObject并实现接口Castle.Core.Logging.ILogger

     using System;
    using System.Globalization;
    using ILogger = Castle.Core.Logging.ILogger;
    using NLogCore = NLog; namespace Abp.Castle.Logging.NLog
    {
    [Serializable]
    public class NLogLogger :
    MarshalByRefObject,
    ILogger
    {
    protected internal NLogCore.ILogger Logger { get; set; }
    //protected internal NLogLoggerFactory Factory { get; set; } public NLogLogger(NLogCore.ILogger logger)
    {
    Logger = logger;
    } internal NLogLogger()
    {
    } public bool IsDebugEnabled => Logger.IsEnabled(NLogCore.LogLevel.Debug); public bool IsErrorEnabled => Logger.IsEnabled(NLogCore.LogLevel.Error); public bool IsFatalEnabled => Logger.IsEnabled(NLogCore.LogLevel.Fatal); public bool IsInfoEnabled => Logger.IsEnabled(NLogCore.LogLevel.Info); public bool IsWarnEnabled => Logger.IsEnabled(NLogCore.LogLevel.Warn); public ILogger CreateChildLogger(string loggerName)
    {
    return new NLogLogger(NLogCore.LogManager.GetLogger(Logger.Name + "." + loggerName));
    } public void Debug(string message)
    {
    Logger.Debug(message);
    } public void Debug(Func<string> messageFactory)
    {
    Logger.Debug(messageFactory);
    } public void Debug(string message, Exception exception)
    {
    Logger.Debug(exception, message);
    } public void DebugFormat(string format, params object[] args)
    {
    Logger.Debug(CultureInfo.InvariantCulture, format, args);
    } public void DebugFormat(Exception exception, string format, params object[] args)
    {
    Logger.Debug(exception, CultureInfo.InvariantCulture, format, args);
    } public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Debug(formatProvider, format, args);
    } public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Debug(exception, formatProvider, format, args);
    } public void Error(string message)
    {
    Logger.Error(message);
    } public void Error(Func<string> messageFactory)
    {
    Logger.Error(messageFactory);
    } public void Error(string message, Exception exception)
    {
    Logger.Error(exception, message);
    } public void ErrorFormat(string format, params object[] args)
    {
    Logger.Error(CultureInfo.InvariantCulture, format, args);
    } public void ErrorFormat(Exception exception, string format, params object[] args)
    {
    Logger.Error(exception, CultureInfo.InvariantCulture, format, args);
    } public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Error(formatProvider, format, args);
    } public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Error(exception, formatProvider, format, args);
    } public void Fatal(string message)
    {
    Logger.Fatal(message);
    } public void Fatal(Func<string> messageFactory)
    {
    Logger.Fatal(messageFactory);
    } public void Fatal(string message, Exception exception)
    {
    Logger.Fatal(exception, message);
    } public void FatalFormat(string format, params object[] args)
    {
    Logger.Fatal(CultureInfo.InvariantCulture, format, args);
    } public void FatalFormat(Exception exception, string format, params object[] args)
    {
    Logger.Fatal(exception, CultureInfo.InvariantCulture, format, args);
    } public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Fatal(formatProvider, format, args);
    } public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Fatal(exception, formatProvider, format, args);
    } public void Info(string message)
    {
    Logger.Info(message);
    } public void Info(Func<string> messageFactory)
    {
    Logger.Info(messageFactory);
    } public void Info(string message, Exception exception)
    {
    Logger.Info(exception, message);
    } public void InfoFormat(string format, params object[] args)
    {
    Logger.Info(CultureInfo.InvariantCulture, format, args);
    } public void InfoFormat(Exception exception, string format, params object[] args)
    {
    Logger.Info(exception, CultureInfo.InvariantCulture, format, args);
    } public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Info(formatProvider, format, args);
    } public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Info(exception, formatProvider, format, args);
    } public void Warn(string message)
    {
    Logger.Warn(message);
    } public void Warn(Func<string> messageFactory)
    {
    Logger.Warn(messageFactory);
    } public void Warn(string message, Exception exception)
    {
    Logger.Warn(exception, message);
    } public void WarnFormat(string format, params object[] args)
    {
    Logger.Warn(CultureInfo.InvariantCulture, format, args);
    } public void WarnFormat(Exception exception, string format, params object[] args)
    {
    Logger.Warn(exception, CultureInfo.InvariantCulture, format, args);
    } public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Warn(formatProvider, format, args);
    } public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
    {
    Logger.Warn(exception, formatProvider, format, args);
    }
    }
    }
  5. 添加工厂类NLogLoggerFactory并实现抽象类Castle.Core.Logging.AbstractLoggerFactory
     using Castle.Core.Logging;
    using System;
    using System.IO;
    using NLogCore = NLog; namespace Abp.Castle.Logging.NLog
    { public class NLogLoggerFactory : AbstractLoggerFactory
    {
    internal const string DefaultConfigFileName = "nlog.config";
    //private readonly ILoggerRepository _loggerRepository; public NLogLoggerFactory()
    : this(DefaultConfigFileName)
    { } public NLogLoggerFactory(string configFileName)
    {
    if (!File.Exists(configFileName))
    {
    throw new FileNotFoundException(configFileName);
    }
    NLogCore.LogManager.Configuration = new NLogCore.Config.XmlLoggingConfiguration(configFileName);
    } public override ILogger Create(string name)
    {
    if (name == null)
    {
    throw new ArgumentNullException(nameof(name));
    }
    return new NLogLogger(NLogCore.LogManager.GetLogger(name));
    } public override ILogger Create(string name, LoggerLevel level)
    {
    throw new NotSupportedException("Logger levels cannot be set at runtime. Please review your configuration file.");
    }
    }
    }
  6. 添加LoggingFacility的扩展方法UseAbpNLog
     using Castle.Facilities.Logging;
    
     namespace Abp.Castle.Logging.NLog
    {
    public static class LoggingFacilityExtensions
    {
    public static LoggingFacility UseAbpNLog(this LoggingFacility loggingFacility)
    {
    return loggingFacility.LogUsing<NLogLoggerFactory>();
    }
    }
    }
  7. 移除Abp.Castle.Log4Net包,添加Abp.Castle.NLog到Host项目
  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"
    autoReload="true"
    internalLogLevel="Warn"
    internalLogFile="App_Data\Logs\nlogs.txt"> <variable name="logDirectory" value="${basedir}\log\"/> <!--define various log targets-->
    <targets> <!--write logs to file-->
    <target xsi:type="File" name="allfile" fileName="${logDirectory}\nlog-all-${shortdate}.log"
    layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="ownFile-web" fileName="nlog-my-${shortdate}.log"
    layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="Null" name="blackhole" /> </targets> <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
    </nlog>
  9. 修改Startup, 将原来的日志组件log4net替换为nlog

    注释using Abp.Castle.Logging.Log4Net; 添加using Abp.Castle.Logging.NLog;

     //using Abp.Castle.Logging.Log4Net;
    using Abp.Castle.Logging.NLog;

    修改ConfigureServices方法

      // Configure Abp and Dependency Injection
    return services.AddAbp<AbpBasicWebHostModule>(
    // Configure Log4Net logging
    //options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
    // f => f.UseAbpLog4Net().WithConfig("log4net.config")
    //) // Configure Nlog Logging
    options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
    f => f.UseAbpNLog().WithConfig("nlog.config")
    )
    );
  10. 测试
     public IActionResult Index()
    {
    //nlog test
    Logger.Info("信息日志");
    Logger.Debug("调试日志");
    Logger.Error("错误日志");
    Logger.Fatal("异常日志");
    Logger.Warn("警告日志");
    return Redirect("/swagger");
    }

    测试结果

ABP .Net Core 日志组件集成使用NLog的更多相关文章

  1. net core体系-web应用程序-4net core2.0大白话带你入门-7asp.net core日志组件(Logger和Nlog)

    asp.net core日志组件   日志介绍 Logging的使用 1. appsettings.json中Logging的介绍 Logging的配置信息是保存在appsettings.json配置 ...

  2. .Netcore之日志组件Log4net、Nlog性能比较

    转载请注明出处http://www.cnblogs.com/supernebula/p/7506993.html .Netcore之Log4net.Nlog性能比较 最近在写一个开源.netcore ...

  3. 【框架学习与探究之日志组件--Log4Net与NLog】

    前言 本文欢迎转载,作者原创地址:http://www.cnblogs.com/DjlNet/p/7604340.html 序 近日,天气渐冷,懒惰的脑虫又开始作祟了,导致近日内功修炼迟迟未能进步,依 ...

  4. .Net core2.0日志组件Log4net、Nlog简单性能测试

    .Net core之Log4net.Nlog简单性能测试 比较log4net.nlog的文件写入性能(.netcore环境),涉及代码和配置如有不正确的地方,还请批评指正. 原创,转载请著名出处:ht ...

  5. asp.net core日志组件

    日志介绍 Logging的使用 1. appsettings.json中Logging的介绍 Logging的配置信息是保存在appsettings.json配置文件中的.因为之前介绍配置文件的时候我 ...

  6. 基于DDD的.NET开发框架 - ABP日志Logger集成

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  7. 玩转ASP.NET Core中的日志组件

    简介 日志组件,作为程序员使用频率最高的组件,给程序员开发调试程序提供了必要的信息.ASP.NET Core中内置了一个通用日志接口ILogger,并实现了多种内置的日志提供器,例如 Console ...

  8. [.Net Core] 在 Mvc 中简单使用日志组件

    在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...

  9. Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

    本文目录 1. Net下日志记录 2. NLog的使用     2.1 添加nuget引用NLog.Web.AspNetCore     2.2 配置文件设置     2.3 依赖配置及调用     ...

随机推荐

  1. linux安装redis(转)

    一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...

  2. Yacc 与 Lex 快速入门(词法分析和语法分析)

    我们知道,高级语言,一般的如c,Java等是不能直接运行的,它们需要经过编译成机器认识的语言.即编译器的工作. 编译器工作流程:词法分析.语法分析.语义分析.IR(中间代码,intermediate ...

  3. BP算法从原理到python实现

    BP算法从原理到实践 反向传播算法Backpropagation的python实现 觉得有用的话,欢迎一起讨论相互学习~Follow Me 博主接触深度学习已经一段时间,近期在与别人进行讨论时,发现自 ...

  4. MyEclipse 使用图文详解

    引言 某天在群里看到有小伙伴问MyEclipse/Eclipse的一些使用问题,虽然在我看来,问的问题很简单,但是如果对于刚刚学习的人来说,可能使用就不那么友好了.毕竟我在开始使用MyEclipse/ ...

  5. js二级事件模型的处理细节

    一.纠正网络上的一个误传--“IE不支持事件捕获” 可以在浏览器中运行上面demo,在各主流浏览器中,鼠标移上都可以分别触发捕获与冒泡事件的监听函数,所以IE也是支持事件捕获的,连IE6都支持,只是在 ...

  6. 用js解析XML文件,字符串一些心得

    解析XML文件遇到的问题 今天秦博士叫我解析一下XML文件,将里面的所有的X坐标Y坐标放在一个数组里面然后写在文档里让他进行算法比对,大家都知道了啦,解析XML文件获取里面的坐标数据什么的,当然是用前 ...

  7. 【hdu5419】Victor and Toys

    求求求 搞搞搞 搞法例如以下:考虑每一个数w[i]w[i]对答案的贡献,呃. . .首先答案一定是 ∑[...](m3) \sum [...]\over {m\choose 3}的形式,仅仅须要搞分子 ...

  8. WPF使用RoutedCommand自己定义命令

    主要代码例如以下所看到的: /// <summary> /// 声明并定义命令. /// </summary> RoutedCommand ClearCommand = new ...

  9. Express4.x API (一):application (译)

    Express4.x API 译文 系列文章 Express4.x API (一):application (译) -- 完成 Express4.x API (二):request (译) -- 完成 ...

  10. JAVA入门[14]-Spring MVC AOP

    一.基本概念 1.AOP简介 DI能够让相互协作的软件组件保持松散耦合:而面向切面编程(aspect-oriented programming,AOP)允许你把遍布应用各处的功能分离出来形成可重用的组 ...