Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active May 19, 2023 08:53
Show Gist options
  • Save akunzai/a49ae487a413f521c44e3846edf10059 to your computer and use it in GitHub Desktop.
Save akunzai/a49ae487a413f521c44e3846edf10059 to your computer and use it in GitHub Desktop.
My nlog config
{
// https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"extensions": [
{
"assembly": "NLog.Extensions.Logging"
},
{
"assembly": "NLog.Web.AspNetCore"
}
],
"variables": {
// in Azure App Service the logging base-directory should be D:\home\LogFiles
"LOG_PATH": "${environment:LOG_PATH:whenEmpty=${basedir:fixtempdir=true}}"
},
"targets": {
"async": true,
"trace": {
"type": "Trace"
},
"console": {
"type": "ColoredConsole"
},
"file": {
// https://github.com/nlog/NLog/wiki/File-target
"type": "File",
"layout": "${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:|${exception:maxInnerExceptionLevel=5}}",
"encoding": "utf-8",
"fileName": "${var:LOG_PATH}/log.txt",
"archiveFileName": "${var:LOG_PATH}/${date:format=yyyy-MM}/log.{#}.txt",
"archiveEvery": "Day",
"archiveNumbering": "Date",
"archiveDateFormat": "yyyy-MM-dd",
"maxArchiveDays": 90,
"keepFileOpen": true,
"concurrentWrites": false,
"openFileCacheTimeout": 30
},
"mail": {
// https://github.com/NLog/NLog/wiki/LimitingWrapper-target
"type": "LimitingWrapper",
"interval": "01:00",
"messageLimit": 5,
"target": {
// https://github.com/NLog/NLog/wiki/Mail-target
"type": "Mail",
"smtpServer": "${environment:SMTP_HOST:whenEmpty=smtp.sendgrid.net}",
"smtpPort": 587,
"smtpAuthentication": "Basic",
"smtpUserName": "${environment:SMTP_USER:whenEmpty=apikey}",
"smtpPassword": "${environment:SMTP_PASS:whenEmpty=${environment:SENDGRID_API_KEY}}",
"from": "${environment:LOG_MAIL_FROMNAME} <${environment:LOG_MAIL_FROM}>",
"to": "${environment:LOG_MAIL_TO}",
"subject": "Error Notification from ${environment:LOG_HOSTNAME:whenEmpty=${hostname}}",
"body": "${longdate}|${logger}|${message}${newline}${exception:maxInnerExceptionLevel=5}",
"addNewLines": true,
"priority": "High"
}
}
},
// https://github.com/nlog/NLog/wiki/Configuration-file#rules
"rules": {
"1": {
// Output hosting lifetime messages to console target for faster startup detection
"logger": "Microsoft.Hosting.Lifetime",
"minLevel": "Info",
"writeTo": "console",
"final": true
},
// Skip non-critical logs (BlackHole)
"2": {
"logger": "Microsoft.*",
"maxLevel": "Info",
"final": true
},
// log level: Trace|Debug|Info|Warn|Error|Fatal
"3": {
"logger": "*",
"minLevel": "Trace",
"writeTo": "trace"
},
"4": {
"logger": "*",
"minLevel": "Info",
"writeTo": "file",
"enabled": false
},
"5": {
"logger": "*",
"minLevel": "Error",
"writeTo": "mail",
"enabled": false
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<!-- https://nlog-project.org/config/ -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Extensions.Logging" />
<add assembly="NLog.Web.AspNetCore" />
</extensions>
<!-- in Azure App Service the logging base-directory should be D:\home\LogFiles -->
<variable name="LOG_PATH" value="${environment:LOG_PATH:whenEmpty=${basedir:fixtempdir=true}}" />
<targets async="true">
<target name="trace" xsi:type="Trace" />
<target name="console" xsi:type="ColoredConsole" />
<!-- https://github.com/nlog/NLog/wiki/File-target -->
<target name="file" xsi:type="File"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:|${exception:maxInnerExceptionLevel=5}}"
encoding="utf-8"
fileName="${var:LOG_PATH}/log.txt"
archiveFileName="${var:LOG_PATH}/${date:format=yyyy-MM}/log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Date"
archiveDateFormat="yyyy-MM-dd"
maxArchiveDays="90"
keepFileOpen="true" concurrentWrites="false" openFileCacheTimeout="30" />
<!-- https://github.com/NLog/NLog/wiki/LimitingWrapper-target -->
<target name="mail" xsi:type="LimitingWrapper" messageLimit="5" interval="01:00">
<!-- https://github.com/NLog/NLog/wiki/Mail-target -->
<target xsi:type="Mail"
smtpServer="${environment:SMTP_HOST:whenEmpty=smtp.sendgrid.net}"
smtpPort="587" smtpAuthentication="Basic"
smtpUserName="${environment:SMTP_USER:whenEmpty=apikey}"
smtpPassword="${environment:SMTP_PASS:whenEmpty=${environment:SENDGRID_API_KEY}}"
from="${environment:LOG_MAIL_FROMNAME} &lt;${environment:LOG_MAIL_FROM}&gt;"
to="${environment:LOG_MAIL_TO}"
subject="Error Notification from ${environment:LOG_HOSTNAME:whenEmpty=${hostname}}"
body="${longdate}|${logger}|${message}${newline}${exception:maxInnerExceptionLevel=5}"
addNewLines="true"
priority="High" />
</target>
</targets>
<!-- https://github.com/nlog/NLog/wiki/Configuration-file#rules -->
<rules>
<!-- Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="console" final="true" />
<!-- Skip non-critical logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- log level: Trace|Debug|Info|Warn|Error|Fatal -->
<logger name="*" minlevel="Trace" writeTo="trace" />
<logger name="*" minlevel="Info" writeTo="file" enabled="false" />
<logger name="*" minlevel="Error" writeTo="mail" enabled="false" />
</rules>
</nlog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment