[walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json
说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化。
首先,理论部分看[汤姆大叔的博客] 解读ASP.NET 5 & MVC6系列(9):日志框架
实际上aspnet的开发人员已经在最近版的系统里开始集成nlog了。 本文的目的主要帮助大家理解aspnet mvc 6的框架。
新建工程 "NlogTest"
选“asp.net 5”的”web site”, 然后不要认证模块,我们主要演示NLog的用法,对auth认证没兴趣。
◎添加Nlog参照
打开project.json, 添加 NLog,同时删除dnxcore50,Nlog还没有支持coreclr,所以先删了。
修改frameworks部分,修改后的样子。
"frameworks": {
"dnx451": {
"dependencies": { "NLog": "3.2.0" }
}
},
◎添加nlog的配置到config.json里,5行以下为本次追加内容。
{
"AppSettings": {
"SiteTitle": "NLogTest"
},
"nlog": {
"targets": {
"file": {
"type": "File",
"layout": "${date:format=HH\\:MM\\:ss} ${logger} ${message}",
"fileName": "c:\\work\\aaa.txt"
},
"file2": {
"type": "File",
"fileName": "c:\\work\\bbb.txt"
}
"mail1": { }
},
"rules": {
"rule1": {
"minlevel": "Debug",
"writeTo": "file"
},
"rule2": {
"name": "*",
"minlevel": "Info",
"writeTo": "file2"
}
}
}
}
配置到此为止,下面开始编程。
◎添加一个NLogProvider类, 实现ILoggerProvider并在其内部实现ILogger
先上代码,我是微软的实现中搬过来的,做了一些修改。
using Microsoft.Framework.Logging;
using System; namespace NLogTest
{
public class NLogProvider:ILoggerProvider
{
private readonly global::NLog.LogFactory _logFactory; public NLogProvider(global::NLog.LogFactory logFactory)
{
_logFactory = logFactory;
} public ILogger CreateLogger(string name)
{
return new Logger(_logFactory.GetLogger(name));
} private class Logger : ILogger
{
private readonly global::NLog.Logger _logger; public Logger(global::NLog.Logger logger)
{
_logger = logger;
} public IDisposable BeginScope(object state)
{
return global::NLog.NestedDiagnosticsContext.Push(state.ToString());
} public bool IsEnabled(LogLevel logLevel)
{
return _logger.IsEnabled(GetLogLevel(logLevel));
} public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
var nLogLogLevel = GetLogLevel(logLevel);
var message = string.Empty;
if (formatter != null)
{
message = formatter(state, exception);
}
else
{
message = LogFormatter.Formatter(state, exception);
} if (!string.IsNullOrEmpty(message))
{
var eventInfo = global::NLog.LogEventInfo.Create(nLogLogLevel, _logger.Name, message);
eventInfo.Properties["EventId"] = eventId;
_logger.Log(eventInfo);
}
} private global::NLog.LogLevel GetLogLevel(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Verbose: return global::NLog.LogLevel.Debug;
case LogLevel.Information: return global::NLog.LogLevel.Info;
case LogLevel.Warning: return global::NLog.LogLevel.Warn;
case LogLevel.Error: return global::NLog.LogLevel.Error;
case LogLevel.Critical: return global::NLog.LogLevel.Fatal;
}
return global::NLog.LogLevel.Debug;
}
}
}
}
代码很简单,就是在微软的日志框架和NLog的函数间实现一个桥接。
◎添加一个NLogLoggerFactoryExtensions类,追加ILoggerFactory的扩张函数,这里是本次演示的重点了。
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.Logging;
using System;
using System.Linq;
using System.Text; namespace NLogTest
{
public static class NLogLoggerFactoryExtensions
{
public static ILoggerFactory AddNLog(
this ILoggerFactory factory,
IConfiguration configuration)
{
var config = new global::NLog.Config.LoggingConfiguration(); var targets = configuration.GetSubKey("targets"); foreach (var item in targets.GetSubKeys())
{
AddTargets(config, item.Key, item.Value);
} var rules = configuration.GetSubKey("rules");
foreach (var item in rules.GetSubKeys())
{
AddLoggingRule(config, item.Value);
} factory.AddProvider(new NLogProvider(new global::NLog.LogFactory(config)));
return factory;
} private static void AddTargets(global::NLog.Config.LoggingConfiguration configuration, string targetName, IConfiguration targetConf)
{
string targetType = "";
if (targetConf.TryGet("type", out targetType))
{
switch (targetType.ToLower())
{
case "file":
configuration.AddTarget(targetName, GenFileTarget(targetName, targetConf));
break;
case "mail":
configuration.AddTarget(targetName, GenMailTarget(targetName, targetConf));
break;
default:
break;
}
}
} private static global::NLog.Targets.Target GenFileTarget(string targetName, IConfiguration targetConf)
{
var fileTarget = new global::NLog.Targets.FileTarget();
fileTarget.Name = targetName; string confVal = GetVal(targetConf, "fileName");
if (string.IsNullOrEmpty(confVal))
{
//Filename is not setting , throw exception!
throw new ArgumentNullException("fileTarget's filename is empty.");
} fileTarget.FileName = confVal; confVal = GetVal(targetConf, "layout");
if (!string.IsNullOrEmpty(confVal))
{
fileTarget.Layout = confVal;
} confVal = GetVal(targetConf, "keepfileopen");
if (!string.IsNullOrEmpty(confVal))
{
fileTarget.KeepFileOpen = (confVal.ToLower() == "true");
} confVal = GetVal(targetConf, "encoding");
if (!string.IsNullOrEmpty(confVal))
{
fileTarget.Encoding = Encoding.GetEncoding(confVal);
} fileTarget.AutoFlush = true; return fileTarget;
} private static global::NLog.Targets.Target GenMailTarget(string targetName, IConfiguration targetConf)
{
var mailTarget = new global::NLog.Targets.MailTarget();
mailTarget.Name = targetName; string confVal = GetVal(targetConf, "to");
if (string.IsNullOrEmpty(confVal))
{
//to is not setting , throw exception!
throw new ArgumentNullException("mailTarget's [to] is empty.");
} mailTarget.To = confVal; confVal = GetVal(targetConf, "from");
if (string.IsNullOrEmpty(confVal))
{
//to is not setting , throw exception!
throw new ArgumentNullException("mailTarget's [from] is empty.");
} mailTarget.From = confVal; confVal = GetVal(targetConf, "layout");
if (!string.IsNullOrEmpty(confVal))
{
mailTarget.Layout = confVal;
} confVal = GetVal(targetConf, "subject");
if (!string.IsNullOrEmpty(confVal))
{
mailTarget.Subject = confVal;
} confVal = GetVal(targetConf, "smtpusername");
if (!string.IsNullOrEmpty(confVal))
{
mailTarget.SmtpUserName = confVal;
}
confVal = GetVal(targetConf, "smtppassword");
if (!string.IsNullOrEmpty(confVal))
{
mailTarget.SmtpPassword = confVal;
} confVal = GetVal(targetConf, "smtpserver");
if (!string.IsNullOrEmpty(confVal))
{
mailTarget.SmtpServer = confVal;
} confVal = GetVal(targetConf, "smtpport");
if (!string.IsNullOrEmpty(confVal))
{
int nPort = ;
if (int.TryParse(confVal, out nPort))
{
mailTarget.SmtpPort = nPort;
}
} return mailTarget;
} private static void AddLoggingRule(global::NLog.Config.LoggingConfiguration configuration, IConfiguration ruleConf)
{
string namePattern = "*";
string confVal = GetVal(ruleConf, " name");
if (!string.IsNullOrEmpty(confVal))
{
namePattern = confVal;
} confVal = GetVal(ruleConf, "minlevel");
global::NLog.LogLevel minLevel = global::NLog.LogLevel.Debug;
if (!string.IsNullOrEmpty(confVal))
{
minLevel = GetLogLevel(confVal, global::NLog.LogLevel.Trace);
} confVal = GetVal(ruleConf, "writeto");
global::NLog.Targets.Target target = null;
if (!string.IsNullOrEmpty(confVal))
{
target = configuration.ConfiguredNamedTargets.Where(t => t.Name == confVal).FirstOrDefault();
} if (target != null)
{
configuration.LoggingRules.Add(new global::NLog.Config.LoggingRule(namePattern, minLevel, target));
}
} private static string GetVal(IConfiguration configuration, string key)
{
string val = "";
if (configuration.TryGet(key, out val))
{
return val;
}
else
{
return null;
}
} private static global::NLog.LogLevel GetLogLevel(string logLevel, global::NLog.LogLevel defaultLevel = null)
{
switch (logLevel.ToLower())
{
case "debug": return global::NLog.LogLevel.Debug;
case "info": return global::NLog.LogLevel.Info;
case "warn": return global::NLog.LogLevel.Warn;
case "error": return global::NLog.LogLevel.Error;
case "fatal": return global::NLog.LogLevel.Fatal;
}
return defaultLevel;
}
}
}
实例化Nlog.LogFactory类,并从config里读取的配置,设置到该LogFactory里。
下面是NLog的使用了。
◎打开 Startup.cs文件,并在Configure函数里AddLog。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
{
// Configure the HTTP request pipeline. // Add the console logger.
loggerfactory.AddConsole(); //Add the NLog logger
loggerfactory.AddNLog(Configuration.GetSubKey("nlog")); //Log Output Test.
var logger = loggerfactory.CreateLogger("NLogTest");
logger.LogInformation("this is infomation from startup"); try
{
var i = - ;
var j = / i;
}
catch (DivideByZeroException ex)
{
logger.LogError("error log test", ex);
} 。。。。。。。。。。以下略 }
◎在Controller里使用
打开HomeController.cs文件,并追加代码。
public class HomeController : Controller
{
private ILogger _logger = null; public HomeController(ILoggerFactory logFactory)
{
_logger = logFactory.CreateLogger(nameof(HomeController)); _logger.LogWarning("I am created.");
} public IActionResult Index()
{
_logger.LogWarning("hello from index of home control... ");
return View();
}
・・・・・・・・・・・以下略
}
编译通过后,F5一下,看看自己的成果吧。
本文完结。
另外,俺不会提供完整的project代码,大家还是自己敲吧,因为偷懒的木匠从来都不是好司机。
[walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json的更多相关文章
- 解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理
在前面的章节中,我们知道新版的MVC程序抛弃了原来的web.config文件机制,取而代替的是config.json,今天我们就来深入研究一下配置文件的相关内容. 基本用法 新版的配置信息机制在Mic ...
- 认识ASP.NET MVC6
认识ASP.NET MVC6 这篇文章说明下如何在普通编辑器下面开发mvc6应用程序. 上篇文章: 十分钟轻松让你认识ASP.NET 5(MVC6) 首先安装mvc6的nuget包: 可以看到在pro ...
- ASP.NET5 MVC6入门教学之一(自己动手)
等待微软开源大动作已经好几年了,终于ASP.NET 5发布了.今天给新手们写一个简单地教程,教你认识一下ASP.NET5 MVC6 1.安装kvm 首先,你需要以管理员权限打开cmd,执行如下的脚本: ...
- asp.net mvc6学习资料整理
十分钟轻松让你认识ASP.NET MVC6 http://www.cnblogs.com/n-pei/p/4272105.html ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web ...
- [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件
本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ASP.NET Core . ASP.NET Core已经内置了日志支持,可以 ...
- ASP.NET Core 实战:使用 NLog 将日志信息记录到 MongoDB
一.前言 在项目开发中,日志系统是系统的一个重要组成模块,通过在程序中记录运行日志.错误日志,可以让我们对于系统的运行情况做到很好的掌控.同时,收集日志不仅仅可以用于诊断排查错误,由于日志同样也是大量 ...
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
EF Core使用SQL调用返回其他类型的查询 假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...
- 在web.config里使用configSource分隔各类配置
转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...
随机推荐
- 【转】java 文件 读取目录下的所有文件(包括子目录)
转自:http://www.cnblogs.com/pricks/archive/2009/11/11/1601044.html import java.io.File; import java.io ...
- GWT(Google Web Tookit) Eclipse Plugin的zip下载地址(同时提供GWT Designer下载地址)
按照Eclipse Help->Install new software->....(这里是官方安装文档:http://code.google.com/intl/zh-CN/eclipse ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...
- Linux Resin 安装
1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...
- [Redux] Adding React Router to the Project
We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...
- 吴柄锡 github----MHA helper
https://github.com/wubx http://www.cnblogs.com/kissdb/p/4009620.html
- Building Tomcat7 source step by step---官方文档
Table of Contents Introduction Download a Java Development Kit (JDK) version 6 Install Apache Ant 1. ...
- iOS-UITableCell详情
iOS-UITableCell详情 表示UITableViewCell风格的常量有: UITableViewCellStyleDefault UITableViewCellStyleSubtitle ...
- myeclipse一些技巧
ctrl+h-----------------查找字符串 ctrl+A 全选→ctrl+shift+f 代码格式化,排版 ctrl+shift+O 自动引用