Nuget引用

NLog.Web.AspNetCore

注册

using Microsoft.Extensions.Logging;
using NLog.Web;
using NLog.Extensions.Logging;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//引入Nlog配置文件
env.ConfigureNLog("nlog.config");
//使用NLog作为日志记录工具
loggerFactory.AddNLog();
...
}

调用

using Microsoft.Extensions.Logging;
using NLog;

private Logger _logger;

public BondNewsController(YfjkDbContext context,
ILogger<BondNewsController> logger)
{
_db = context;
_logger = LogManager.GetCurrentClassLogger();
_logger.Debug("BondNewsController Init!!!!!!!!!!!!!!!");
}

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="logs/all/nlog-all-${shortdate}.log"
layout="${longdate}||${logger}||${uppercase:${level}}||${message} ${exception}" />-->
<!--<target name="ownFile-web" xsi:type="File" fileName="logs/my/nlog-my-${shortdate}.log"
layout="${longdate}||${logger}||${uppercase:${level}}||${message} ${exception}" />

<target name="error_file" xsi:type="File"
fileName="logs/my/nlog-error-${shortdate}.log" maxArchiveFiles="30"
layout="${longdate} || ${level:uppercase=false} || ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}}" />-->
<target xsi:type="Null" name="blackhole" />
<target name="allfile" xsi:type="File" fileName="logs/all/nlog-all-${shortdate}.log" encoding="utf-8">
<layout xsi:type="JsonLayout">
<attribute name="@timestamp" layout="${longdate}" />
<attribute name="level" layout="${uppercase:${level}}"/>
<attribute name="logger" layout="${logger}" />
<attribute name="content" layout="${message}" escapeUnicode="false"/>
<attribute name="exceptionmessage" layout="${exception:format=Message}" escapeUnicode="false"/>
<attribute name="stacktrace" layout="${exception:format=StackTrace}" escapeUnicode="false"/>
</layout>
</target>
<target name="info-file" xsi:type="File" fileName="logs/info/nlog-info-${shortdate}.log" >
<layout xsi:type="JsonLayout">
<attribute name="@timestamp" layout="${longdate}" />
<attribute name="level" layout="${uppercase:${level}}"/>
<attribute name="timelytype" layout="${event-properties:timelytype}" />
<attribute name="timelycontent" layout="${event-properties:timelycontent}" escapeUnicode="false"/>
<attribute name="logger" layout="${logger}" />
<attribute name="content" layout="${message}" escapeUnicode="false"/>
</layout>
</target>
<target name="error_file" xsi:type="File" fileName="logs/info/nlog-error-${shortdate}.log" encoding="utf-8">
<layout xsi:type="JsonLayout">
<attribute name="@timestamp" layout="${longdate}" />
<attribute name="level" layout="${uppercase:${level}}"/>
<attribute name="timelytype" layout="${event-properties:timelytype}" />
<attribute name="timelycontent" layout="${event-properties:timelycontent}" escapeUnicode="false"/>
<attribute name="logger" layout="${logger}" />
<attribute name="content" layout="${message}" escapeUnicode="false"/>
<attribute name="exceptionmessage" layout="${exception:format=Message}" escapeUnicode="false"/>
<attribute name="stacktrace" layout="${exception:format=StackTrace}" escapeUnicode="false"/>
</layout>
</target>
<target name="coloredConsole" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false"
layout="${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${message}" >
<highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />
<highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" />
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />
</target>
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" level="Info" writeTo="info-file" />
<logger name="*" level="Error" writeTo="error_file" />
<logger name="*" minlevel="Trace" writeTo="coloredConsole" />
</rules>
</nlog>

添加自定义的日志内容

<attribute name="@timestamp" layout="${longdate}" />
<attribute name="level" layout="${uppercase:${level}}"/>
<attribute name="timelytype" layout="${event-properties:timelytype}" />
<attribute name="timelycontent" layout="${event-properties:timelycontent}" escapeUnicode="false"/>
<attribute name="logger" layout="${logger}" />
<!--由于使用filebeat 这里不能使用message作为key-->
<attribute name="content" layout="${message}" escapeUnicode="false"/>
<attribute name="exceptionmessage" layout="${exception:format=Message}" escapeUnicode="false"/>
<attribute name="stacktrace" layout="${exception:format=StackTrace}" escapeUnicode="false"/>

扩展

using Castle.Core.Logging;
using NLog;
using System;
using System.Collections.Generic;
using System.Text;
namespace EntityRiskUiBackendApi.Extensions
{
/// <summary>
/// NetCore Nlog扩展
/// </summary>
public static class NLoggerExtensions
{
private static NLog.ILogger nlogger;
static NLoggerExtensions()
{
nlogger = LogManager.GetCurrentClassLogger();
}
/// <summary>
/// 自定义日志
/// </summary>
/// <param name="logger"></param>
/// <param name="message"></param>
/// <param name="keyValuePairs">nlog配置里的event-properties自定义项</param>
public static void CustomInfoLog(this Microsoft.Extensions.Logging.ILogger logger, string message, Dictionary<string, object> keyValuePairs)
{
if (keyValuePairs != null)
{
LogEventInfo theEvent = new LogEventInfo(LogLevel.Info, logger.ToString(), message);
foreach (var item in keyValuePairs)
{
theEvent.Properties[item.Key] = item.Value;
}
nlogger.Log(theEvent);
}
}
/// <summary>
/// 往钉钉推送的消息
/// </summary>
/// <param name="logger"></param>
/// <param name="message"></param>
public static void CustomInfoLogToDingDing(this Microsoft.Extensions.Logging.ILogger logger, string message)
{
Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
keyValuePairs.Add("timelytype", "dingding");
keyValuePairs.Add("timelycontent", message);
logger.CustomInfoLog(message, keyValuePairs);
}
}
}

扩展调用

_logger.CustomInfoLogToDingDing($"裁判系统用户{userName}登录成功了");