Reputation: 19181
I am looking for an efficient and generic design that encapsulates a log message.
I often find myself writing the same logger.XXXFormat()
many times and I would like to encapsulate those messages into objects.
What i'm thinking is that I should use logMessage.ToString()
to retrieve the log message from the object, but how do I encapsulate the formatting itself in a generic way so when given an object of type T it will extract the relevant data and format the message?
Upvotes: 0
Views: 149
Reputation: 22394
Are you saying that you're often logging some formatted string built from the internals of an object instance? For example, you've got this Address
type and you're always doing...
Log.InfoFormat("{0}; {1}, {2} {3}", address.Line1, address.City, address.State, address.Zip);
If so, then I have two recommendations.
My friend, John Nelson (AKA John Coder), has a library for taking "stencils" and filling them in with object instances.
The geniuses of the FubuMVC project have released their core projects separately. They deal with formatting and printing objects all the time. Check out their classes.
Upvotes: 2