1. NuGet包管理器或包管理器控制台命令安装NLog和NLog.Mongo
Install-Package NLog.Web.AspNetCore
Install-Package NLog.Mongo
2. 配置appsettings.json文件NLog项
"NLog": {
"throwConfigExceptions": true,
"targets": {
"logfile": {
"type": "File",
"fileName": "./log/nlog-${shortdate}.log"
},
"logconsole": {
"type": "Console"
},
"logmongo":{
"type":"Mongo",
"connectionString":"mongodb://192.168.111.2/NLOG",
"collectionName":"SysErrorLog",
"cappedCollectionSize":"26214400"
}
},
"extensions": {
"NLog.Mongo": {
"assembly": "NLog.Mongo"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "logmongo"
},
{
"logger": "*",
"minLevel": "Warn",
"writeTo": "logfile"
},
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "logconsole"
}
]
}
3. 修改Startup.cs文件ConfigureServices,添加如下代码
NLog.LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
loggingBuilder.AddNLog(Configuration);
});
网友评论