一、说明

  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. ngixn配置

    nginx 配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一头雾水. 今天看到个文档不错,翻译过来分享给大家,可以让新手更详细地了解nginx配置 ...

  2. 360提供的php防注入代码

    <?php //Code By Safe3 function customError($errno, $errstr, $errfile, $errline) { echo "< ...

  3. 高质量PHP代码的50个实用技巧必备(上)

    1.不要使用相对路径 常常会看到: ? 1 require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. ...

  4. The authenticity of host 'github.com (192.30.253.113)' can't be established.

    在初始化git之后(git init),同时在github建立好仓库之后,本地也新增了ssh kye(ssh-keygen -t rsa -C 'mail address'),同时也在本地新增了远程仓 ...

  5. Linux多线程编程——线程的同步

    POSIX信号量 posix信号量不同于IPC中的信号量  常用的posix信号量函数   #include <semaphore.h> int sem_init(sem_t* sem,i ...

  6. 翻煎饼 Stacks of Flapjacks

    题意:本题意为煎饼排序,大的放在上面,小的放在下面(此题输入是从上到下输入的),为煎饼排序是通过一系列的"翻转"动作来完成的.翻转动作就是将一个小铲插到一叠煎饼中的某两个煎饼之间, ...

  7. 再说Postgres中的高速缓存(cache)

    表的模式信息存放在系统表中,因此要访问表,就需要首先在系统表中取得表的模式信息.对于一个PostgreSQL系统来说,对于系统表和普通表模式的访问是非常频繁的.为了提高这些访问的效率,PostgreS ...

  8. MySQL系列:基于binlog的增量订阅与消费(一)

    在一些业务场景中,像在数据分析中我们有时候需要捕获数据变化(CDC):在数据审计中,我们也往往需要知道数据从这个点到另一个点的变化:同样在实时分析中,我们有时候需要看到某个值得实时变化等. 要解决以上 ...

  9. Spring 面试

    1.什么是Spring框架?Spring框架有哪些主要模块? spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...

  10. JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题

    JAVA基础再回首(二十五)--Lock锁的使用.死锁问题.多线程生产者和消费者.线程池.匿名内部类使用多线程.定时器.面试题 版权声明:转载必须注明本文转自程序猿杜鹏程的博客:http://blog ...