1.安装nuget
2.添加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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<!--输出到文件-->
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} [${level:uppercase=true}] ${message}" />
<!--VS输出窗口-->
<target name="debugger" xsi:type="Debugger" layout="${longdate} [${level:uppercase=true}] ${message}"/>
<!--输出到控制台-->
<target name="console" xsi:type="Console" layout="${longdate} [${level:uppercase=true}] ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="f" />
<logger name="*" writeTo="debugger"/>
<logger name="*" writeTo="console"/>
</rules>
</nlog>
3.添加logger帮助类
using NLog;
namespace MES.Service.Api.Comm
{
public class Logger
{
NLog.Logger _logger { get; set; }
private Logger(NLog.Logger logger)
{
_logger = logger;
}
public Logger(string name) : this(LogManager.GetLogger(name))
{
}
public static Logger Default { get; private set; }
static Logger()
{
Default = new Logger(LogManager.GetCurrentClassLogger());
}
#region Dedub
public void Debug(string msg, params object[] args)
{
_logger.Debug(msg, args);
}
public void Debug(string msg, Exception e)
{
_logger.Debug(e, msg);
}
#endregion
#region Info
public void Info(string msg, params object[] args)
{
_logger.Info(msg, args);
}
public void Info(string msg, Exception e)
{
_logger.Info(e, msg);
}
#endregion
#region Trace
public void Trace(string msg, params object[] args)
{
_logger.Trace(msg, args);
}
public void Trace(string msg, Exception e)
{
_logger.Trace(e, msg);
}
#endregion
#region Warn
public void Warn(string msg, params object[] args)
{
_logger.Warn(msg, args);
}
public void Warn(string msg, Exception e)
{
_logger.Warn(e, msg);
}
#endregion
#region Error
public void Error(string msg, params object[] args)
{
_logger.Error(msg, args);
}
public void Error(string msg, Exception e)
{
_logger.Error(e, msg);
}
#endregion
}
}
4.在program中添加映射
//添加路由映射
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
5.使用