Skip to content

Simple log4net Objects appender - extract any data that you want from your object.

License

Notifications You must be signed in to change notification settings

urielha/SimpleObjectAppender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleObjectAppender

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

A log4net forwarding appender (example) which will format your object as string and send it to next appenders.

Usage example:

 <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.

SimpleObjectConverter

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.

Usage example:

 <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>

SimpleObjectConsoleAppender

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.

Usage example:

 <appender name="objectConsoleAppender" type="SimpleObjectAppender.SimpleObjectConsoleAppender, SimpleObjectAppender">
    <Details>
      <Property>prop1</Property>
      <Property>prop2</Property>
    </Details>
  </appender>

About

Simple log4net Objects appender - extract any data that you want from your object.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages