SignalR HubConnection.TraceWriter using Event Log

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 = newHubConnection(strURLToMyHub);
hub.TraceWriter =
neweventLogTraceWriter(eventLog1);

 

The final code for the TraceWriter

publicclasseventLogTraceWriter : System.IO.TextWriter {

       publicEventLog _eventLog;

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

              _eventLog = eventLog;

       }

       publicoverridevoid WriteLine(string value) {

              _eventLog.WriteEntry(value);

       }

       publicoverrideEncoding Encoding {

              get { returnEncoding.UTF8; }

       }

}

Pretty simple...

For more complete information about compiler optimizations, see our Optimization Notice.