Simple log4net extensions - extract any data that you want from your object.
The idea of the ObjectAppender was originally brought by @eliranmoyal and @DimaRabkin.
This specific project started as a simple and abstract example of how can this be achieved and I decided to upload it.
The SimpleObjectAppender gives you the ability to:
// Say you have this class:
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
// So INSTEAD of logging like this:
logger.Info(string.Format("Person details - Name:{0}, Age:{1}", person.Name, person.Age))
// You would do this:
logger.Info(person);
And choose which properties of the class you want to log and in which format inside your log4net configuration.
Project contains:
- SimpleObjectForwarder - Forwarding Appender.
- SimpleObjectConverter - Layout Converter.
- SimpleObjectConsoleAppender - Console Appender.
A log4net forwarding appender (example) which will format your object as string and send it to next appenders.
<appender name="objectForwarder" type="SimpleObjectAppender.SimpleObjectForwarder, SimpleObjectAppender">
<appender-ref ref="nextAppenderName" />
<Details>
<Property>Prop1Name</Property>
<Property>Prop2Name</Property>
</Details>
</appender>
More Tags:
- Separator - The separator string between each property. (default: ", ")
- EqualsSymbol - The string which separate between property name and value. (default: ":")
- IgnoreNotExists - Ignore the whole key+value if the property doesn't exists. (default: true)
- Descriptor - If you are pro and want custom property extractor.
A log4net pattern layout converter (example of creating and using converter) which will give you the ability to format the message with your own object properties.
<appender name="MyConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<Converter>
<name value="object" />
<type value="SimpleObjectAppender.SimpleObjectConverter, SimpleObjectAppender" />
</Converter>
<conversionPattern value="%-4timestamp %-5level %logger - Name is:%object{Name}, age:%object{Age}%newline" />
</layout>
</appender>
A log4net console appender (example) - just a console appender, the configuration is exactly as the forwarding appender except that this appender will just write to you console instead of forwarding the message.
<appender name="objectConsoleAppender" type="SimpleObjectAppender.SimpleObjectConsoleAppender, SimpleObjectAppender">
<Details>
<Property>prop1</Property>
<Property>prop2</Property>
</Details>
</appender>