1

Objective

Log the errors to Microsoft Teams with NLog from a console program.

Issue

Was able to log to the console, but not to Teams

Code

Config:

<targets>
    <target xsi:type="Console" name="console"/>
    <target xsi:type="WebService"
         name="microsoft-teams"
         url="https://outlook.office.com/webhook/abcd"
         protocol='JsonPost' 
         encoding='UTF-8' 
         includeBOM='false' >
    </target>
</targets>
<rules>
    <logger name="*" minlevel="Info" writeTo="console" />
    <logger name='*' writeTo='microsoft-teams' />
</rules>

Logging code:

private static Logger logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args) {

    logger.Error("{'text':'test'}");
    logger.Fatal("Sample fatal error message");

}

The console target displayed the errors very well. But the Teams channel didn't have log, the nlog-internal.log showed

System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

Any insight would be greatly appreciated!

Julian
  • 33,915
  • 22
  • 119
  • 174
Quentin
  • 1,310
  • 18
  • 30

2 Answers2

4

You need at least send some parameters, see example webserver target:

<target type='WebService'
        name='ws'
        url='http://localhost:1234/logme'
        protocol='HttpPost'
        encoding='UTF-8'   >
    <parameter name='param1' type='System.String' layout='${message}'/> 
    <parameter name='param2' type='System.String' layout='${level}'/>
</target>

If this still isn't working, I would recommend checking the call with Fiddler

Julian
  • 33,915
  • 22
  • 119
  • 174
  • That was the example I followed, how do I use the params? Btw, the call works fine in Fiddler/Postman – Quentin May 26 '17 at 13:55
  • Turned out I named the layout the same name with param1, apparently they are predefined. Now it works! Thank you @Julian – Quentin May 26 '17 at 14:19
  • I was able to send a string via `logger.Fatal("Test");` but can't send the json format like `logger.Fatal(new ComplextType{Text = "test", Title="test title"});` Seems I need this https://github.com/NLog/NLog/issues/1905, but is there a workaround? – Quentin May 26 '17 at 16:06
3

I know it is a little bit late, but you can try the NLog extension I created. https://github.com/jedipi/NLog.Targets.MicrosoftTeams

Example Output

jedipi
  • 155
  • 4