Custom NLog Target for sending log events via SMS using Twilio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.ComponentModel.DataAnnotations; | |
namespace Twilio.Targets | |
{ | |
[NLog.Targets.Target("Sms")] | |
public class Sms : NLog.Targets.TargetWithLayout | |
{ | |
[Required] | |
public string AccountSid { get; set; } | |
[Required] | |
public string AuthToken { get; set; } | |
[Required] | |
public string From { get; set; } | |
[Required] | |
public string To { get; set; } | |
protected override void Write(NLog.LogEventInfo logEvent) | |
{ | |
string logMessage = this.Layout.Render(logEvent); | |
SendTheMessageToTheRemoteHost(logMessage); | |
} | |
private void SendTheMessageToTheRemoteHost(string message) | |
{ | |
string msg = (message.Length > 160) ? message.Substring(0, 160) : message; | |
var client = new TwilioRestClient(this.AccountSid, this.AuthToken); | |
client.SendSmsMessage( | |
this.From, | |
this.To, | |
msg | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment