As we needed a way to send our logs from a Windows CE device in near real time to an off-machine logging window, we decided that logging to a serial port would be the simplest option (our device has 8 serial ports!). The software already uses log4net for logging, and the best way was to use an appender that outputs to a serial port.
Since I couldn’t find an appender that writes to a COM port, I had to write my own. It was very easy, since log4net provides most of the functionality in an object called an AppenderSkeleton, from which my COMAppender is derived.
Right now the code is very simplistic – data is pumped out the serial port without regard to errors or flow control. Testing has been minimal (only on Windows CE, but it should also work on Windows 32/64). Only two settings can be specified in the appender configuration:
- PortNumber (an integer number; in Windows CE a number less than 10 will create the string COMx, a number greater than 9 will generate the device name: $device\COMx).
- BaudRate (an integer number, I’ve tested only with 115200).
To add this appender into the configuration file, specify the following:
<appender name="SerialAppender"
type="PowerSoft.log4net.Appender.COMAppender, COMAppender, version=1.0.0.0, culture=neutral, PublicKeyToken=null">
<PortNumber value="10"/>
<BaudRate value="115200"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />
</layout>
</appender>
Obviously the specifics of PortNumber, BaudRate and conversionPattern is up to you.
Source code is now on codeplex at http://comappender.codeplex.com/
Print | posted on Tuesday, January 25, 2011 11:47 AM