Size concious method for logging in C#
I have a program that report exceptions to my server. I'd like to also send a complete lo开发者_如何学JAVAg of the current session when the exception happens. Since this is a bug report, I'd like to condense it to a tidy little datastream with one or two characters per action. Examples of actions are things like: Clicks File Menu, Opens new spreadsheet, saves current workspace. One method might be to make a key where say each letter in the alphabet represents a particular action, then decode it on my server. This might be problematic if I want to add strings to the log though. (Example: Opens File "example.txt") Is there perhaps a more object oriented approach to this that doesn't sacrifice size and automates the encoding/decoding? I'm just brainstorming here so any suggestions or recommendations are most welcome. Thanks!
Have you considered whether you need to be "size conscious" at all. Here's a trot through some metrics:
Supposed you have 1000 errors per day, and suppose each error takes up 1k of data. That's about 1MB per day. After a yeah you're going to be at around 350MB of error reporting. OK.
They're fictional numbers but in any case those kinds of metrics seems trivial to me especially since they're also rather overblown... Are you expecting more than 1000 error events per day? Are your individual errors going to be larger than 1k? Are you logging to an embedded device? Probably not.
My opinion is:
You may use exceptions for any purpose, in general, but as you are considering to send exceptions to the server, then you might be using exceptions to represent a programming fault, that is, something you'd be concerned about.
These kinds of exceptions are meant to be rare, your software won't have a dozen of them and throw many times a day. If so, why do you have to be compress so much the information youre sending to the server? Why don't you just GZIP the callstack along with some log4net logging and send it to the server? You'll save time.
I'd like to condense it to a tidy little datastream with one or two characters per action
The easiest thing is to save what you want in some convenient (e.g. plain text) format, and then compress it using some standard compression tool: let the compression algorithm work out how to compress it.
I would just use the command pattern and a persistent background thread sitting in a loop calling the .Execute() method on all the objects coming in to be processed. then you get the benefit of being able to throttle the db writes, more easily batch db writes, and maybe even use bcp to transfer the data. you can always archive/delete old exception info too.
精彩评论