NLog target and layout renderer for ntfy.sh 🪵
This repository contains a library containing the code for the NLog target and layout renderer, and also a test and demo console library.
Table of Contents
Use Visual Studio 2022 to build the project.
Get the NuGet package from nuget.org or search for MichelMichels.NLog.Targets.Ntfy
in the GUI package manager in Visual Studio.
You can also use the cli of the package manager with following command:
Install-Package MichelMichels.NLog.Targets.Ntfy
⚠️ This assumes you already have anlog.config
file. For more information to setup NLog, see the NLog wiki.
These instructions will add information to your NLog configuration file to make our target available. In this setup, the default nlog.config is used and nothing is removed.
ℹ️ For brevity, only the added or edited elements are displayed in these code snippets.
<extensions>
<add assembly="MichelMichels.NLog.Targets.Ntfy" />
</extensions>
<targets>
<target xsi:type="Ntfy" name="logntfy" />
</targets>
In this snippet, logntfy
(the name of the target configured above) is added to the writeTo
attribute. The others are default and are not necessary for this target to work.
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile,logconsole,logntfy" />
</rules>
After these steps, you can subscribe to the nlog-ntfy
topic and you will see the notifications come in. More information is included below to change the default settings, but you can use this as-is. I only recommend to change the topic attribute in the target configuration to something only you know. See Configuration > Topic for instructions.
ℹ️ The default value for the host is
https://ntfy.sh/
.
If you want to use another ntfy-server, you can change the host
attribute on the target.
Example:
<target xsi:type="Ntfy" name="logntfy" host="https://other.ntfy.server/" />
ℹ️ The default value for the title is
NLog
.
The notification title contains 3 parts:
- An emoji linked to the LogLevel (tags)
- The string-value of the LogLevel (f.e. 'Debug')
- The value in the 'Title' attribute (f.e. your app name)
The notification title of your Ntfy notification for a debug log message with default configuration would be:
:computer: Debug - NLog
In previous example, the NLog
part will be changed when setting the Title
attribute on the target.
You can change the title icons (or 'tags') by setting the *Tags
attributes. See the emoji shortcodes on ntfy.sh for supported emoji-tags. See the defaults table for the default values.
ℹ️ The default value for the topic is
nlog-ntfy
.
⚠️ You should change the topic if you don't want other people to read your logging.
Set the topic
attribute on the target to change to a ntfy topic of your choice. In the example below, the topic is changed to my-app-logging
.
Example:
<target xsi:type="Ntfy" name="logntfy" topic="my-app-logging" />
These are the default values when nothing is changed.
Setting | Value |
---|---|
Host | https://ntfy.sh/ |
Topic | nlog-ntfy |
Title | NLog |
TraceTags | empty |
DebugTags | 💻, computer |
InformationTags | ℹ️, information_source |
WarnTags | warning |
ErrorTags | ❗, exclamation |
FatalTags | 💀, skull |
DefaultTags (fallback) | empty |
Any layout renderer can be used, but I also included one. The body of the notification will be the render output. To use the renderer included in this package you can add following attribute and value to the Ntfy target:
layout="${ntfy}"
This produces following output:
Date: dd/mm/yyyy
Time: hh:mm
{logMessage}
Exception: {exception.ToString()}
StackTrace: {stackTrace}
The exception and stacktrace output is only enabled if this info is present in the log event.
Date and time output can be disabled by using the isdaterendered
option on the layoutrenderer.
layout="${ntfy:isdaterendered=false}"
Coming soon™
Written by Michel Michels.