NLog.Targets.Journald is a custom target for NLog.
It outputs systemd-journald
native protocol to unix domain socket at /run/systemd/journal/socket
.
It can be used with version 4.7.13 and later.
- Add reference to
NLog.Targets.Journald
NuGet package in your project. - Modify your application configuration to load and use
Journald
target.
Parameter | Optional/Mandatory | Description |
---|---|---|
Layout |
Mandatory | Used to render journal record MESSAGE field from NLog event. |
SysLogIdentifier |
Optional | Useful for log filtering with journalctl -t <syslog identifier> . |
StaticFields |
Optional | Zero, one or more additional static fields output to journal, accessible using journalctl -o verbose or journalctl KEY=VALUE |
See NLog docs for more information.
<extensions>
<add assembly="NLog.Targets.Journald"/>
</extensions>
<targets>
<target xsi:type="Journald" name="journald">
<!-- mandatory -->
<layout>"${logger} ${message}"</layout>
<!-- optional -->
<sysLogIdentifier>my-service</sysLogIdentifier>
<!-- optional, multiple allowed -->
<!-- Key must consist of upper case letters, numbers and underscores only.
It is always converted to upper case. -->
<static-field key="KEY_1" value="value 1" />
<static-field key="KEY_2" value="value 2" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="journald" />
</rules>
NLog.config is simple but complete example.
For ASP.NET Core, .NET Core, .NET5 ant later projects. See NLog.Extensions.Logging for more information.
"NLog": {
"extensions": [
{ "assembly": "NLog.Targets.Journald" }
],
"targets": {
"journald": {
"type": "Journald",
"layout": "${logger} ${message}",
"syslogIdentifier": "my-service",
"staticFields": [
{ "Key": "KEY_1", "Value": "value 1" },
{ "Key": "KEY_2", "Value": "value 2" }
],
}
},
"rules": {
"10": {
"logger": "*",
"minLevel": "Trace",
"writeTo": "journald"
}
}
}
NLog log events are emmited to Journald using the following fields.
Journal field | Field type | Description |
---|---|---|
PRIORITY |
User | NLog LogLevel mapped to journal priority value between 2 (crit ) and 7 (debug ). |
MESSAGE |
User | Same as ${message} in NLog layout config. |
LEVEL |
Custom | Same as ${level} in NLog layout config. |
LOGGER |
Custom | Same as ${logger} in NLog layout config. |
TIMESTAMP |
Custom | NLog event timestamp as ISO 8601 string. Example: 2022-02-11T15:30:47+00:00 . |
STACKTRACE |
Custom | NLog event StackTrace property as string. |
EXCEPTION_TYPE |
Custom | NLog event exception type name. Example: System.ArgumentOutOfRangeException . |
EXCEPTION_MESSAGE |
Custom | NLog event exception Message property. |
EXCEPTION_STACKTRACE |
Custom | NLog event exception StackTrace property. |
More info about Journal fields.
NLog LogLevel | Journal priority decimal | Journal priority string |
---|---|---|
Fatal |
2 | crit |
Error |
3 | err |
Warn |
4 | warning |
Info |
6 | info |
Debug |
7 | debug |
Trace |
7 | debug |
NLog.Targets.Journald is licensed under the terms of MIT license.
Please see the LICENSE file for further information.
Unix domain socket support is implemented using UnixEndPoint class from Mono platform library.