DEV Community

Cover image for using NLog to send log message to MS Teams Channel via O365 Webhook Connector

Posted on


using NLog to send log message to MS Teams Channel via O365 Webhook Connector


I recently created a NLog target extension to write log message to Microsoft Teams Channel.
NLog Targets for Microsoft Teams

Sending log messages to Microsoft Teams Channel is a good way to notify the development team or the service desk team if there is anything happened in your application. It helps proactively find out the issues sooner.

Below are some benefits to send log message to MS Teams

  • Log message shows up in Teams instantly
  • All team members get notified
  • Log messages are searchable with Teams

Let's Code

First, let's create a console app using Visual Studio. I named the project as NLogTeams, and targeting .NET 6.
NLog Targets for Microsoft Teams testing console app

Then we need to install the necessary NuGet package. Just right click on the Solution and click on Manage NuGet Packages for Solution.
nuget packages

Type in NLog.Targets.MicrosoftTeams in the search box, and install it. All the depended packages will be installed to the project as well.
NLog Targets MicrosoftTeams NuGet Package

Now let's add a new xml file to the project, and name this new file as nlog.config.
nlog config

Open up the nlog.config file, copy & paste the code below and save.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="" xsi:schemaLocation="NLog NLog.xsd"
      internalLogLevel="Off" >

  <!-- the targets to write to -->
    <!-- write logs to Microsoft Teams -->
    <target xsi:type="MicrosoftTeams, NLog.Targets.MicrosoftTeams"
            WebhookUrl="Put your Teams Webhook URL here"
            ApplicationName="Your Application Name"
            CardTitle="Title - ${level:uppercase=true}: ${date} - [${logger}]"
            layout="[${level:uppercase=true}] ${logger} - ${message} ${all-event-properties}"

  <!-- rules to map from logger name to target -->
    <logger name="*" minlevel="Trace" writeTo="msTeams" />
Enter fullscreen mode Exit fullscreen mode

You need to get the MS Teams Channel webhook URL. Please follow the Microsoft's instruction to create the incoming webhook in Teams.

Once you have the webhook URL, all you need to do is paste it into the nlog.config file.
Image description

And make sure the nlog.config file is always Copy to Output Directory
Image description

Finally, you can now send log message to your Teams Channel. Let's test it by replacing the code in the Program.cs with the code below.

using NLog;

public class MyClass
    static ILogger _logger = LogManager.GetCurrentClassLogger();
    public static void TestMethod()
        Console.WriteLine("Hello World!");
        _logger.Info("Log to MS Teams channel like a boss");
Enter fullscreen mode Exit fullscreen mode

That's it. And the result would be like this.
Image description


Many businesses are leveraging Microsoft Teams for collaboration. I hope this helps you to integrate your application with Microsoft Teams to receive Error notification easily.

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<