I made a few improvements to the Logging TraceListener:
String.Format codes for file number and date
Here's how the new options look when set it up via the .config file:
<!-- this enables IIS-style logging of console output -->
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="CyclicLog"
type="ConsoleApp.CyclicLogTraceListener,ConsoleApp"
initializeData="fileCountThreshold=10, fileSizeThreshold=512,
fileSizeUnit=kilobytes, fileAgeThreshold=1, fileAgeUnit=months,
fileNameTemplate='console-{1:MMM-yy}-{0:0000}.log'" />
</listeners>
</trace>
</system.diagnostics>
And here's how it looks when created manually:
_HtmlLogger = New CyclicLogTraceListener
With _HtmlLogger
.TimeStampFormat = ""
.FileNameTemplate = "html-{1:MMM-yy}-{0:0000}.log"
.FileAgeThreshold = 1
.FileAgeUnit = CyclicLogTraceListener.AgeUnit.Months
.FileCountThreshold = 3
End With
I did this to get a seperate, private log file for the large amounts of HTML I wanted to trace. Including them with the regular console output is way too noisy. I combined age, number, and size limits, so I end up with something like this:
html-Feb-05-0000.log (1mb) html-Feb-05-0001.log (1mb) html-Mar-05-0000.log (1mb) console-Feb-05-0000.log (512kb) console-Mar-05-0000.log (512kb)
With the configuration specified above, no more than 3 logs will ever exist, and no more than 3 per month, and never more than 512kb or 1mb in size.
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.
Jeff Atwood on March 12, 2005 2:56 AMHere'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
Oliver Sturm on March 12, 2005 6:32 AMWell, 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..
Jeff Atwood on March 13, 2005 8:57 AMWell, 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.
Oliver Sturm on March 13, 2005 10:11 AMI'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.
Oliver Sturm on March 14, 2005 4:35 AMI 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.
Andy Hall on July 19, 2005 11:50 AMWell, 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.
The comments to this entry are closed.
|
|
Traffic Stats |