• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!



Page history last edited by Corey Alix 14 years, 10 months ago

Twitter Trace Listener

Thanks to the Yedda Twitter C# Library I was able to port a subset of the code to create a TwitterTraceListener.


VB.NET Source Code:


Option Explicit On

Option Strict On

Option Infer On


Imports System.Net


Public Class TwitterTraceListener

    Inherits TraceListener


    Protected Overrides Function GetSupportedAttributes() As String()

        Return New String() {"username", "password"}

    End Function


    <Flags()> Private Enum ActionType

        friends = 1

        update = 2

    End Enum


    <Flags()> Private Enum ObjectType

        account = 1

        statuses = 2

        users = 4

    End Enum


    <Flags()> Private Enum FormatType

        atom = 1

        json = 2

        rss = 4

        xml = 8

    End Enum


    Private m_NetworkCredential As System.Net.NetworkCredential = Nothing

    Private ReadOnly Property NetworkCredential() As System.Net.NetworkCredential


            If IsNothing(m_NetworkCredential) Then

                m_NetworkCredential = New System.Net.NetworkCredential

                m_NetworkCredential.UserName = Me.Attributes("username")

                m_NetworkCredential.Password = Me.Attributes("password")

            End If

            Return m_NetworkCredential

        End Get

    End Property


    Private Shared Function TwitterURL(ByVal piObjectType As ObjectType, ByVal piActionType As ActionType, ByVal piFormatType As FormatType) As String

        Return String.Format("http://twitter.com/{0}/{1}.{2}", piObjectType, piActionType, piFormatType)

    End Function


    ' Eliminates the need for System.Web

    Private Shared Function UrlEncode(ByVal piValue As String) As String

        ' return System.Web.HttpUtility.UrlEncode(piValue)

        Dim lvResult As New System.Text.StringBuilder

        For Each lvChar In piValue

            If Char.IsLetterOrDigit(lvChar) Then



                lvResult.AppendFormat("%{0:X02}", AscW(lvChar))

            End If


        Return lvResult.ToString

    End Function


    Private Shared Function TwitterStatus(ByVal piStatus As String) As String

        Return String.Format("status={0}", UrlEncode(piStatus))

    End Function


    Private Function ExecutePostCommand(ByVal piURL As String, ByVal piData As String) As String

#If DEBUG Then

        Dim lvResult = String.Format("piURL={0}, piData={1}", piURL, piData)


        Return lvResult


        System.Net.ServicePointManager.Expect100Continue = False

        Dim lvRequest = WebRequest.Create(piURL)

        lvRequest.Credentials = NetworkCredential

        lvRequest.ContentType = "application/x-www-form-urlencoded"

        lvRequest.Method = "POST"

        Dim lvBytes = System.Text.Encoding.UTF8.GetBytes(piData)

        lvRequest.ContentLength = lvBytes.Length

        Using lvRequestStream = lvRequest.GetRequestStream

            lvRequestStream.Write(lvBytes, 0, lvBytes.Length)

            Using lvResponse = lvRequest.GetResponse

                Using lvResponseStream = lvResponse.GetResponseStream

                    Using lvReader As New System.IO.StreamReader(lvResponseStream)

                        Return lvReader.ReadToEnd

                    End Using

                End Using

            End Using

        End Using

#End If

    End Function


    Private Sub Update(ByVal piStatus As String)

        Dim lvURL = TwitterURL(ObjectType.statuses, ActionType.update, FormatType.xml)

        Dim lvData = TwitterStatus(piStatus)

        Dim lvResult = ExecutePostCommand(lvURL, lvData)

    End Sub


    Private m_Message As New System.Text.StringBuilder

    Public Overrides Sub Write(ByVal message As String)


    End Sub


    Public Overrides Sub WriteLine(ByVal message As String)


    End Sub


    Public Overrides Sub Flush()

        Dim lvLength = Math.Min(160, Me.m_Message.Length)

        If (0 = lvLength) Then Return

        Dim lvStatus = Me.m_Message.ToString(0, lvLength)

        Me.m_Message.Length = 0

        Dim lvWorker = New System.ComponentModel.BackgroundWorker()

        AddHandler lvWorker.DoWork, AddressOf DoWorkHandler


    End Sub


    Private Sub DoWorkHandler(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

        Dim lvStatus = CStr(e.Argument)


    End Sub


End Class


Sample app.config file:






      <source name="DefaultSource" switchName="DefaultSwitch">


          <add name="CriticalTwitterTraceListener"/>





      <add name="DefaultSwitch" value="All"/>



      <add name="CriticalTwitterTraceListener"

               type="YourNamespace.TwitterTraceListenerYourNamespace, Version=, Culture=neutral, PublicKeyToken=null"



        <filter type="System.Diagnostics.EventTypeFilter" initializeData="Critical"/>







Comments (0)

You don't have permission to comment on this page.