什么是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. 使用hexo在github上写blog

    使用hexo在github上写blog 安装nodejs http://nodejs.org/ 安装hexo npm install -g hexo 创建bolg文件夹 安装完成后在自己的工作目录创建 ...

  2. 一个JAVA数据库连接池实现源码

    原文链接:http://www.open-open.com/lib/view/open1410875608164.html // // 一个效果非常不错的JAVA数据库连接池. // from:htt ...

  3. cocos2dx在ubuntu下配置声音引擎

    声音引擎库和cocos2dx的库是分开的我们要使用的时候不得不重新修改一下makefile,首先我们要找到声音引擎库的位置,在cocos2dx的 根目录下有一个lib文件,看一下是否存在libcoco ...

  4. 在unity5中减少Draw Calls(SetPass Calls)[转]

    在unity5中减少Draw Calls(SetPass Calls)   我一直工作于unity5支持的Standard Shader(标准着色器)上,并且做了一些关于如何有效地减少draw cal ...

  5. 解析Javascript中大括号“{}”的多义性

    JS中大括号有四种语义作用 语义1,组织复合语句,这是最常见的 复制代码 代码如下: if( condition ) {   //... }else {   //... } for() {   //. ...

  6. 安卓模拟器bluestack 换imei

    有好多种方法,下面介绍2种   第一种方法   通过靠谱助手设置,非常简单.   第二种方法   1.解压 root_20121221文件夹,将Root.fs 覆盖到 win7路径:C:\Progra ...

  7. C#基础总结之四List-Hashtable-冒泡排序

    #region 第四天作业 名片--------ArrayList //ArrayList card = new ArrayList(); //card.Add("2202111001122 ...

  8. Arcgis报错: Bad login user Failed to execute (CreateEnterpriseGeodatabase).

    在使用工具Create Enterprise Geodatabase的时候报错Bad login user,开始怀疑为密码错误,然后反复在plsql中尝试发现并没有错误,很疑惑,然后去官网查询: Er ...

  9. Linux高级编程--04.GDB调试程序(查看数据)

    查看栈信息 当程序被停住了,你需要做的第一件事就是查看程序是在哪里停住的.当你的程序调用了一个函数,函数的地址,函数参数,函数内的局部变量都会被压入"栈"(Stack)中.你可以用 ...

  10. java模拟一个简单的QQ

    v 项目源码 https://github.com/hjzgg/java_QQ v 标题效果       package testFour; import java.awt.Color; import ...