什么是NLog?

NLog is a free logging platform for .NET with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.

It can process diagnostic messages emitted from any .NET language, augment them with contextual information, format them according to your preference and send them to one or more targets such as file or database.

NLog在GitHub的官网:https://github.com/NLog

NLog for .NET Core:https://github.com/NLog/NLog.Extensions.Logging

创建一个.NET Core Console 应用

使用CLI(Command Line Interface)创建一个example程序:

  • dotnet new
  • dotnet restore
  • dotnet run

这些命令行的具体意思这里就不再赘述。

更改project.json的配置

主要是新增了NLog相关的配置:

  • NLog的包引用;
  • 程序发布时包含NLog的配置文件。

新增NLog的配置文件

在程序的根目录下新增NLog的配置文件: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="internal-nlog.txt"> <!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="ownFile-web" fileName="nlog-own-${shortdate}.log"
layout="${longdate}|${threadid}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}" /> <target xsi:type="Null" name="blackhole" />
</targets> <rules>
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="*" minlevel="Info" writeTo="ownFile-web" />
</rules>
</nlog>

在Startup类中注册NLog的MiddleWare

 using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; namespace ConsoleApplication
{
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Add NLog to ASP.NET Core
loggerFactory.AddNLog();

// configure nlog.config in your project root
env.ConfigureNLog("nlog.config");

app.Run(context =>
{
return context.Response.WriteAsync("Hello World!!");
});
}
}
}

在入口类中记录日志

 using System.Threading;
using Microsoft.AspNetCore.Hosting;
using NLog; namespace ConsoleApplication
{
public class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
logger.Info("Server is running...");
logger.Info(string.Format("Current Thead Id:{0}", Thread.CurrentThread.ManagedThreadId));
var host = new WebHostBuilder().UseKestrel().UseStartup<Startup>().Build();
host.Run();
}
}
}

运行程序后会在主目录下生成2个日志文件:

NLog在.NET Core Console Apps中的简单应用的更多相关文章

  1. 在.NET Core console application中使用User Secrets(用户机密)

    微软很坑地只在Microsoft.NET.Sdk.Web中提供了VS项目右键菜单的"管理用户机密"/"Manage User Secrets"菜单项,在使用Mi ...

  2. .NET CORE——Console中使用依赖注入

    我们都知道,在 ASP.NET CORE 中通过依赖注入的方式来使用服务十分的简单,而在 Console 中,其实也只是稍微绕了个小弯子而已.不管是内置 DI 组件或者第三方的 DI 组件(如Auto ...

  3. asp.net core 5.0 中的 JsonConsole

    asp.net core 5.0 中的 JsonConsole Intro asp.net core 5.0 中日志新增了 JsonConsole,还是输出日志到 Console,但是会应用 Json ...

  4. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  5. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  6. Core 1.0中的管道-中间件模式

    ASP.NET Core 1.0中的管道-中间件模式 SP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middlewar ...

  7. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

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

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

  9. async/await 的基本实现和 .NET Core 2.1 中相关性能提升

    前言 这篇文章的开头,笔者想多说两句,不过也是为了以后再也不多嘴这样的话. 在日常工作中,笔者接触得最多的开发工作仍然是在 .NET Core 平台上,当然因为团队领导的开放性和团队风格的多样性(这和 ...

随机推荐

  1. 我也要学iOS逆向工程--全局变量

    大家好!很久不见了.我之前去音乐学院进修爵士吉他去了.现在回来了.之前我一直在学windows开发和逆向.后来到了音乐学院,老师推荐了1个录音软件叫logic prox.可惜啊!当时我只有个索尼的笔记 ...

  2. [转]android开发之字节顺序

    原文在此 android上C++程序为小端字节顺序,和windows上一样. 而android上的JAVA程序则使用的是大端字节顺序. 用NDK和java SDK 做android程序时发现的问题,记 ...

  3. Visual Studio快捷键 [VS2008/VS2005]快捷键

    VS系统通用快捷键: 1.自动排版(类似VC6中的Alt+F8) 编辑.格式化选定内容 Ctrl + K,Ctrl + F 根据周围的代码行,正确缩进选定的代码行. 2.注释与去掉注释功能. 编辑.注 ...

  4. 配置Pylint for Python3.5

    事件的缘由是因为在Ubuntu16.04 下面安装了Visual Studio Code, 再编辑的时候说需要Pylint来检查语法,我系统的默认的Python 版本是python2,而我现在正在学习 ...

  5. HTML5[3]:中文换行

    保证中文每行第一个字,不会出现标点符号 p { white-space: pre-wrap; }

  6. Docker练习例子:基于 VNCServer + noVNC 构建 Docker 桌面系统

    0. 安装docker 这一步略,网上有好多教程,一般出现装不上的原因,也就是网速问题了,这个我也很难帮你. 1. 下载指定的镜像images docker pull dorowu/ubuntu-de ...

  7. nodejs+express中设置登录拦截器

    在nodejs+express中,采用nodejs后端路由控制用户登录后,为了加强前端的安全性控制,阻止用户通过在浏览器地址栏中输入地址访问后台接口,在app.js中需要加入拦截器进行拦截: /*** ...

  8. DDD:一个朋友对领域驱动的小结

    首先我在一家老板有点关系的小软件公司带领一帮工作一两年的程序员做项目,这里要特别强调的是做项目(差不多是外包,只不过客户群体比较固定),项目就是今天项目A是这个逻辑,明天项目B是那个逻辑,两者之间的业 ...

  9. DDD:聊天笔记

    聚合跟和实体 聚合根是实体. 实体有生命周期,使用标识进行跟踪. 聚合根是全局标识,由仓储或其它服务负责其生命周期. 实体是局部标识,由聚合根负责其生命周期. 为什么能应对复杂度? 纵向.横向.时间维 ...

  10. EF Attach时已存在的处理方式

    如果我们在先前的步骤中读取过数据,如 var list = db.Model.ToList(); 之后再,附加 var o = new Model { Id = 1 }; db.Model.Attac ...