Skip to content

Instantly share code, notes, and snippets.

@devinrader
Created June 22, 2012 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinrader/2973813 to your computer and use it in GitHub Desktop.
Save devinrader/2973813 to your computer and use it in GitHub Desktop.
Custom NLog Target for sending log events via SMS using Twilio
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