Skip to content

MichelMichels/nlog-ntfy

Repository files navigation

NLog target and layout renderer for ntfy.sh 🪵

NuGet Version .NET

NLog logo

Ntfy logo


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

Prerequisites

Building

Use Visual Studio 2022 to build the project.

Installation

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


Getting started

⚠️ This assumes you already have a nlog.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.

1. Add extension

<extensions>
    <add assembly="MichelMichels.NLog.Targets.Ntfy" />
</extensions>

2. Add target

<targets>
    <target xsi:type="Ntfy" name="logntfy" />
</targets>

3. Add or edit rule

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>

4. Done!

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.



Configuration

Host

ℹ️ 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/"  />

Notification title

ℹ️ 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.

Notification title icons/tags

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.

Topic

ℹ️ 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"  />

Defaults

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


LayoutRenderer

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.

Disable date output

Date and time output can be disabled by using the isdaterendered option on the layoutrenderer.

layout="${ntfy:isdaterendered=false}"


Screenshots

Coming soon™



Credits

Written by Michel Michels.