I've written a simple event logging system, which I write to with a function similar to:
void IIncidentLogic.ReportIncident(
string code,
string noun,
string hostname,
DateTimeOffset occurredAt)
Where code
is derived from a set of constants such as VOL_SIZE_INCREASE
, noun
would be the path of the volume, hostname
the computer where it occurred, and occurredAt
when it was detected.
Since code
is a known value, I can automatically set other properties, such as the severity of the event, e.g. is it a warning or an error? I also use code
to query a resource dictionary for a friendly error message to display to users. Right now for this example the error message would be A volume has increased in size
, with the noun
clumsily appended to the error message.
I am looking to improve the readability of the error messages, e.g. in this scenario to be something like The volume system (c:) on computer-name has increased in size from 100GB to 200GB
.
I can think of a couple of ways to do this, and am wondering what is the better practice.
I could pre-build the error message, and pass to the logging function, storing in it's own field. However I want to programmaticly force the message to the be the same, no matter where it's generated. A string message
parameter is too open.
So I could record a dictionary of properties against the report, e.g. {originalSize: '100GB', newSize: '200GB', volume: 'system (c:)'}
.
I could still use resource strings to generate the messages, but format them, e.g. The volume {0} on {1} has increased in size from {2} to {3}.
This allows for i8ln, which is a likely future requirement. But it has a possibility to break legacy data if any part changes, e.g. an additional property is required.
And I could do both: use the properties and string formatting, and store the generated string at the same time.
I could also go as far as having a class per event type, or code, with different constructor parameters, but this certainly feels like overkill.
Or is there a different established pattern for this kind of logging?