Encapsulating a log message
I am looking for an efficient and generic design that encapsulates a log message.
I often find myself writing the samelogger.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 me开发者_如何转开发ssage?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.
Mimeo
My friend, John Nelson (AKA John Coder), has a library for taking "stencils" and filling them in with object instances.
FubuCore Stringifier and IDisplayFormatter
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.
精彩评论