说明一下:本文基于随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代码,大家还是自己敲吧,因为偷懒的木匠从来都不是好司机。