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

  • Whenever you search in PBworks, Dokkio Sidebar (from the makers of PBworks) will run the same search in your Drive, Dropbox, OneDrive, Gmail, and Slack. Now you can find what you're looking for wherever it lives. Try Dokkio Sidebar for free.

View
 

TwitterTraceListener

Page history last edited by Corey Alix 13 years, 12 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

        Get

            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.Append(lvChar)

            Else

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

            End If

        Next

        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)

        MsgBox(lvResult)

        Return lvResult

#Else

        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)

        Me.m_Message.Append(message)

    End Sub

 

    Public Overrides Sub WriteLine(ByVal message As String)

        Me.m_Message.AppendLine(message)

    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

        lvWorker.RunWorkerAsync(lvStatus)

    End Sub

 

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

        Dim lvStatus = CStr(e.Argument)

        Update(lvStatus)

    End Sub

 

End Class

 

Sample app.config file:

 

 

<configuration>

  <system.diagnostics>

    <sources>

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

        <listeners>

          <add name="CriticalTwitterTraceListener"/>

        </listeners>

      </source>

    </sources>

    <switches>

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

    </switches>

    <sharedListeners>

      <add name="CriticalTwitterTraceListener"

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

               username="username"

               password="password">

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

      </add>

    </sharedListeners>

  </system.diagnostics>

</configuration>

 

 

Comments (0)

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