Logging TraceListener Improved

I made a few improvements to the Logging TraceListener:


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/03/logging-tracelistener-improved.html

you do know about log4net

I do, but I wanted something simple and lightweight that does one thing, rather than attempting to be the alpha and omega of logging software.

I don’t think a simple cyclic log should incur another DLL dependency or compiling in helper code that is 4-5x larger than the actual code.

But yeah, if I was writing an entire suite of pluggable listeners, then that would be a warning flag.

Here’s an example of the “complexity breeds discontent” factor of log4net:

http://geekswithblogs.net/flanakin/archive/2004/08/30/10418.aspx

And this alternative, which is supposedly simpler to use and dramatically faster from an ex-Java developer who obviously knew about the Java logging project:

http://www.codeproject.com/csharp/nspring.asp

Evdently there’s a lot of object overhead in log4net:

I already did lots of performance testing, and according to performance numbers posted by the log4j folks themselves, my package is faster than at least log4j-- many times faster. (I haven’t tested on an 800MHz AMD machine, but I did test on a comparable Intel machine.) Not only that, but it’s lots easier to use. Please try it and see; I’d be interested in feedback.

Hey Jeff,

you do know about log4net (http://logging.apache.org/log4net/), don’t you? I’ve been fiddling around with my own trace listeners for a while, before completely abandoning them for log4net a while ago. I’m quite sure it can do everything you’ve been writing about out of the box.

Another thing: thanks for using dp.SyntaxHighlighter! I didn’t know it and it looks fantastic. I’m going to integrate it in my blog ASAP.

Oliver

Well, right, my goal in this case was a single console executable file, using the built in .NET logging facilities; someone who has a MCSP (or whatever they’re calling it these days) should be familiar with the .NET framework’s Debug.Trace… but not necessarily log4net.

I dunno, I just saw it as a small problem with a small solution. Other projects my have different needs, and I’d certainly question what I was doing if I started writing an entire logging framework. I don’t think I’ve done that…

Well, log4net works fine for me and I didn’t find it complicated to set up. It also offers me the flexibility I need to really integrate it into my own application concept, for example I wrote my own listener for the udp appender that can be integrated into my app’s management frontend. I don’t have the expectation to be able to understand how to do that from reading an introductory readme.

Of course structural flexibility comes with a price, but I haven’t found that to be especially high with log4net. I don’t tend to optimise my applications for lowest memory consumption these days, I rather want them to be fit for a particular purpose. Part of that purpose is always to be transparent to the end users, and flexible, configurable, versatile logging is a very important part of that, well worth the cost of a dll and a few tenths of a percent on the performance front.

That said, of course I recognize there are situations where you don’t need all that, this is only a reaction to the more generalised criticism.

I’d certainly question what I was doing if I started writing an entire logging framework. I don’t think I’ve done that…

Hey, don’t get me wrong here. It’s interesting work you’ve been posting about. I remember when I was working on a trace listener, I had a hard time finding information about that on the net.

I liked your implementation a lot. Being a C# kind of guy I’ve ported it. I’ve also added another initialiser called specificCategory. This enables you to trace out with a category and if specificCategory is not of length 0 and and matches the category the listener handles the trace. If the specificCategory is empty then it handles all traces. (I have an app that talks through two com ports and wanted to log them separately whilst maintaining the flexibility. Drop me a line if you want a copy of the port.

Well, log4net works fine for me and I didn’t find it complicated to set up. It also offers me the flexibility I need to really integrate it into my own application concept, for example I wrote my own listener for the udp appender that can be integrated into my app’s management frontend. I don’t have the expectation to be able to understand how to do that from reading an introductory readme.
http://altusclinic.ru

Hey, thanks, this is exactly what I was looking for. However, i spotted one issue with the code I downloaded from your site. The EnforceFileThreshold sub does not correctly calculate the oldest file to delete when you have a file count threshold. It actually keeps deleting the newest trace file.
I changed:
Dim OldestFileDate As DateTime = DateTime.MinValue to = DateTime.UtcNow
then changed:
If fi.CreationTimeUtc > OldestFileDate to be a instead.