NLog在.NET Core Console Apps中的简单应用
2016-07-22 10:41
309 查看
什么是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个日志文件:
相关文章推荐
- Activity的四种启动模式
- JNI学习积累之二 ---- 数据类型映射、域描述符说明
- Swift 发送邮件和发短信
- iOS开发拓展篇—CoreLocation地理编码
- iOS开发拓展篇—CoreLocation定位服务
- iOS打包生成pem文件
- android开发MadiaPlayer 发生IllegalStateException
- Android桌面悬浮窗进阶,QQ手机管家小火箭效果实现
- 相同androidprocess,相同androidsharedUserId探索
- android xlistView 魅族手机Hold问题
- JNI学习积累之一 ---- 常用函数大全
- 使用PagerSlidingTabStrip实现顶部导航栏
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- 关于JAVA中的listener回调机制,以及其在Android中的使用
- iOS水波纹动画详解
- TextView设置超链接、背景颜色、字体颜色等等,Spannable的使用。
- @RequestMapping测试各种访问方式
- Unity全面的面试题(包含答案)
- Swift GCD
- Android应用开发SharedPreferences存储数据的使用方法