一、说明

  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. 利用纯CSS美化checkbox和radio和滑动按钮的实现

    W3C提供的CheckBox和radio的原始样式非常的丑,而且在不同的额浏览器表现还不一样,使用常规的方法添加样式没法进行修改样式 一, 单选按钮 <html> <head> ...

  2. JVM内存模型及垃圾回收的研究总结

    Java内存模型 总的来说就分为两个区域,堆内存(Heap)和非堆内存(No-Heap),非堆内存又称为永久代(Permanent),永久的意思其实是针对于垃圾回收器来说的,表示这部分内容不需要回收. ...

  3. memset的实验

      关于memset的实验 实验一:memset对char数组赋初值 #include<iostream> using namespace std; int main() { char a ...

  4. C#换行 System.Environment.NewLine。

    为保持平台的通用性,可以用系统默认换行符 System.Environment.NewLine.

  5. Linux Rsync备份服务介绍及部署守护进程模式

    rsync介绍 rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份工具 在常驻模式(daemon mode)下,rsync默认监听TCP端口873,以原生rsync传输 ...

  6. textarea高度自适应,随着内容增加高度增加

    $(function(){        $.fn.autoHeight = function(){            function autoHeight(elem){             ...

  7. 【Hdu3555】 Bomb(数位DP)

    Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...

  8. Xamarin.iOS + MvvmCross: UIPickerView data binding, SelectedItemChanged event

    UI initialization: _pickerView = new UIPickerView(); _pickerView.ShowSelectionIndicator = true; _pic ...

  9. shell脚本删除N天前的目录-----附linux和mac上date命令的不同

    背景: 每日构建的东西.按日期放到不同的目录里. 现在天的构建放到2015-06-01里,明天的就放到2015-06-02里,依次类推.时间久了.须要一个脚本删除N天前的目录.(本例中N=7.即删除一 ...

  10. 【cogs 597】【dp】交错匹配

    597. 交错匹配 ★☆ 输入文件:crossa.in 输出文件:crossa.out 简单对照 时间限制:1 s 内存限制:128 MB [问题描写叙述] 有两行自然数. UP[1..N] . DO ...