logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Francine Taylor" <Francine.Tay...@genesis-fs.com>
Subject RE: custom fields?
Date Thu, 02 Oct 2008 17:01:55 GMT
Sure.  Here's how you can create a custom dynamic field:


Put this in your application's log4net setup:


// This is a way of creating a dynamic property (found in

// To use it, put a reference to the property in the appender's
property:  %property{custom1}

// It isn't actually used for anything right now, but I wanted it left
in as an example

log4net.GlobalContext.Properties["custom1"] = new Custom1();

log4net.GlobalContext.Properties["custom2"] = new Custom2();

log4net.GlobalContext.Properties["custom2"] = new Custom3();


And then add this class to your project:


// You can use this class to create an active property to be used in the
log4net configuration


// In your application setup routine you would create the property:

//     log4net.GlobalContext.Properties["sampler"] = new


// Then in the log4net.xml file, a pattern might contain this reference:


using System;

using System.Collections.Generic;

using System.Text;


namespace Genesis.Logging {

    public static class ActiveProperties {

        public static string custom1 = "custom1 value";

        public static string custom2 = "custom2 value";

        public static string custom3 = "custom3 value";



    public class Custom1 {

        public override string ToString() {

            return ActiveProperties.custom1;



    public class Custom2 {

        public override string ToString() {

            return ActiveProperties.custom2;




    public class Custom3 {

        public override string ToString() {

            return ActiveProperties.custom3;






Then, in your InfoFormat method, just set the sampleProperty to whatever
you passed in, just before you log the error.



From: Bill McCormick [mailto:wpmccormick@gmail.com] 
Sent: Thursday, October 02, 2008 9:39 AM
To: log4net-user
Subject: custom fields?



Is it possible to add custom fields to log4net. I'd like to be able to
do something like this:

log.InfoFormat("Custom1", "Custom2", "Custom3", "{0}","Message");

and then have an appender look something like:

<layout type="log4net.Layout.

  <conversionPattern value="%custom1 %custom2 %custom3 %message%newline"



NOTICE: The contents of this e-mail message and any attachments are intended solely for the
addressee(s) named in this message. This communication is intended to be and to remain confidential.
If you are not the intended recipient of this message, or if this message has been addressed
to you in error, please immediately alert the sender by reply e-mail and then delete this
message and its attachments. Do not deliver, distribute or copy this message and/or any attachments
and if you are not the intended recipient, do not disclose the contents or take any action
in reliance upon the information contained in this communication or any attachments.
Thank you

View raw message