一、说明

  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. P1092 虫食算

    题目传送:https://www.luogu.org/problem/show?pid=1092 #include <iostream> #include <cstring> ...

  2. POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 105742 ...

  3. 选择客栈noip2011

    哈,没想到吧.今天居然有两篇(算什么,厕所读物吗 选择客栈 本题的更优解请跳转zt 这题11年,刚改2day. 对于30% 的数据,有 n ≤100: 对于50% 的数据,有 n ≤1,000: 对于 ...

  4. 从setTimeout看js函数执行

    老实说,写这篇文章的时候心里是有点压抑的,因为受到打击了,为什么?就 因为喜欢折腾不小心看到了这个"简单"的函数:        for (var i = 0; i < 5; ...

  5. 将STM32 iap hex文件与app hex文件合并为一个hex文件

    日前公司产品需要增加远程升级功能,boot loader程序写好后交予生产部门使用时他们反馈每个产品程序需要刷写两次(一个boot loader 一个app程序),生产进度变慢浪费时间,于是乎研究如何 ...

  6. 从开源项目看 Python 单元测试

    我觉得以前在我开发程序的时候,除了文档,可能单元测试是另外一个让我希望别人都写,但是自己又一点都不想写的东西.但是,随着开发程序的增多,以及自己对 Bug 的修改的增多,我发现,UT 在很大程度上是对 ...

  7. C#复习资料

    C#期末考试复习题 一.单项选择题(每小题2分,共20分) 1.在类作用域中能够通过直接使用该类的(   )成员名进行访问. A. 私有      B. 公用      C. 保护      D. 任 ...

  8. lua 中pairs 和 ipairs差别

    ipairs 和pairs在lua中都是遍历tbale的函数可是两者有差别 1.pairs遍历table中的全部的key-vale 而ipairs会依据key的数值从1開始加1递增遍历相应的table ...

  9. ASP.NET WebAPI使用Swagger生成测试文档

    ASP.NET WebAPI使用Swagger生成测试文档 SwaggerUI是一个简单的Restful API测试和文档工具.简单.漂亮.易用(官方demo).通过读取JSON配置显示API .项目 ...

  10. Office Add-in 设计规范与最佳实践

    作者:陈希章 发表于 2017年8月6日 引子 离上一篇Office Add-in的文章已经过去了一段时间,期间有去年Office 365 Asia Devday & Hackathon的二等 ...