Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Created January 28, 2021 07:48
Show Gist options
  • Save 0x49D1/91a92215762d9f20610875cd413fcfd9 to your computer and use it in GitHub Desktop.
Save 0x49D1/91a92215762d9f20610875cd413fcfd9 to your computer and use it in GitHub Desktop.
Using NLog WebService target for Telegram example. Send logs to Telegram via https://core.telegram.org/bots/api#sendmessage Telegram Bot API.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="telegramBotToken" value="YOUR_TELEGRAM_BOT_TOKEN"/>
<variable name="telegramChatId" value="YOUR_BOT_CHAT_ID"/>
<targets async="true">
<target xsi:type="WebService"
name="telegram"
url="https://api.telegram.org/bot${telegramBotToken}/sendMessage"
encoding="utf-8" protocol="JsonPost">
<parameter name="">
<layout xsi:type="JsonLayout" includeAllProperties="true">
<attribute name="chat_id" layout="${telegramChatId}"/>
<attribute name="parse_mode" layout="HTML"/>
<attribute name="text"
layout="${longdate}|#${level}|&lt;i&gt;${logger}&lt;/i&gt;|&lt;pre&gt;&lt;code&gt;${message} ${exception:format=@}&lt;/code&gt;&lt;/pre&gt;"/>
</layout>
</parameter>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="telegram"/>
</rules>
</nlog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment