NLog在.NET Core Console Apps中的简单应用
什么是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中的简单应用的更多相关文章
- 在.NET Core console application中使用User Secrets(用户机密)
微软很坑地只在Microsoft.NET.Sdk.Web中提供了VS项目右键菜单的"管理用户机密"/"Manage User Secrets"菜单项,在使用Mi ...
- .NET CORE——Console中使用依赖注入
我们都知道,在 ASP.NET CORE 中通过依赖注入的方式来使用服务十分的简单,而在 Console 中,其实也只是稍微绕了个小弯子而已.不管是内置 DI 组件或者第三方的 DI 组件(如Auto ...
- asp.net core 5.0 中的 JsonConsole
asp.net core 5.0 中的 JsonConsole Intro asp.net core 5.0 中日志新增了 JsonConsole,还是输出日志到 Console,但是会应用 Json ...
- 在.NET Core控制台程序中使用依赖注入
之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...
- ASP.NET Core 1.0 中的依赖项管理
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Core 1.0中的管道-中间件模式
ASP.NET Core 1.0中的管道-中间件模式 SP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middlewar ...
- 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持
HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...
- [.Net Core] 在 Mvc 中简单使用日志组件
在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...
- async/await 的基本实现和 .NET Core 2.1 中相关性能提升
前言 这篇文章的开头,笔者想多说两句,不过也是为了以后再也不多嘴这样的话. 在日常工作中,笔者接触得最多的开发工作仍然是在 .NET Core 平台上,当然因为团队领导的开放性和团队风格的多样性(这和 ...
随机推荐
- Linux网络流量实时监控ifstat iftop命令详解
ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB /s i ...
- oracle vm virtualbox 如何让虚拟机可以上网
刚安装了虚拟机,系统linux2.6. 可是想安装一些软件,发现没法联网.郁闷~ 还要手动设置网络,可是也不是小白就可以干的事情,还要弄清楚原理才行. http://reverland.bitbuc ...
- shell来start、stop、restart应用程序模板
这里使用shell中的case语法: case分支语句格式如下: case $变量名 in 模式1) 命令列表 ;; 模式2) 命令列表 ;; *) ;; esac case行尾必须为单词“in”,每 ...
- Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined
I'm stucked in configuring my web.config file under a web forms project in order to get an instance ...
- Java版的Quartz表达式生成器,同时适用于Quartz.net(免费下载)
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...
- spring 启动流程
AbstractApplicationContext 分析 启动流程 // Prepare this context for refreshing.prepareRefresh(); 1. // In ...
- Linux SSH 连接不上
http://blog.csdn.net/cryhelyxx/article/details/46473783 在xshell下用ssh登录远程主机centos出现以下问题: Connection e ...
- STL中vector小结
()使用vector之前必须包含头文件<vector>:#include<vector> ()namespace std{ template <class T, clas ...
- SVN上传代码
概述 SVN上传代码 使用SVN工具上传代码到新浪SAE 1.下载SVN工具http://tortoisesvn.net/downloads.html 2.创建一个文件夹,Checkout,输入SAE ...
- Unity3D 角色(物体) 移动方法 合集
1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...