SignalR HubConnection.TraceWriter using Event Log

By Kevin Pirkl,

Published:10/10/2013   Last Updated:10/10/2013

I needed a better way to Trace what my Windows Service was doing while communicating with my SignalR Hub.  Looking over HubConnection.TraceWriter it turns out that it's pretty easy to roll your own Event Log Writer..

After creating an instance of your HubConnection you set the TraceWriter like so

System.Diagnostics.EventLog eventLog1;
etc..

HubConnection hub = new HubConnection(strURLToMyHub);
hub.TraceWriter =
new eventLogTraceWriter(eventLog1);

 

The final code for the TraceWriter

 

public class eventLogTraceWriter : System.IO.TextWriter {

       public EventLog _eventLog;

       public eventLogTraceWriter(EventLog eventLog) : base(System.Globalization.CultureInfo.InvariantCulture) {

              _eventLog = eventLog;

       }

       public override void WriteLine(string value) {

              _eventLog.WriteEntry(value);

       }

       public override Encoding Encoding {

              get { return Encoding.UTF8; }

       }

}

Pretty simple...

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804