You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Bill McCormick <wp...@gmail.com> on 2008/10/02 18:39:15 UTC

custom fields?

Hello,

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.PatternLayout">
  <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
</layout>


Thanks,

Bill

RE: custom fields?

Posted by Georg Jansen <Ge...@Faktnet.com>.
Bill,

 

Add your custom columns to one of the properties collections, for example
GlobalContext (or ThreadContext), for example:

 

log4net.GlobalContext.Properties["CustomCol1"] = "My custom column 1 value";

log4net.GlobalContext.Properties["CustomCol2"] = "My custom column 1 value";

 

Then pick up the value in the layout with the %property{propertyName}
syntax, for example:

 

%property{CustomCol1}

 

BR,

Georg

 <http://www.l4ndash.com> http://www.l4ndash.com - Log4Net Dashboard

 

 

From: Bill McCormick [mailto:wpmccormick@gmail.com] 
Sent: 2. oktober 2008 18:39
To: log4net-user
Subject: custom fields?

 

Hello,

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.

PatternLayout">
  <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
</layout>


Thanks,

Bill


Re: custom fields?

Posted by Michael Schall <mi...@gmail.com>.
Take a look at the documentation around contexts and how they override each
other...  Hopefully this will give you want you want...
http://logging.apache.org/log4net/release/manual/contexts.html

We use both the GlobalContext and the ThreadContext and they work great.

Mike

On Thu, Oct 2, 2008 at 1:31 PM, Bill McCormick <wp...@gmail.com>wrote:

> Thanks, so much Francine!!
>
> If I understand correctly, once the GC property is set, it would stay set
> for every logged message thereafter? It would actually be better if it would
> be reset, since I would need (potentially) new values with each new logged
> message.
>
> Thanks,
>
>
> Bill
>
> On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor <
> Francine.Taylor@genesis-fs.com> wrote:
>
>>  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
>> ActiveProperties.cs)
>>
>> // 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 SampleProperty();
>>
>> //
>>
>> // Then in the log4net.xml file, a pattern might contain this reference:
>> %property{sampler}
>>
>> //
>>
>> 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?
>>
>>
>>
>> Hello,
>>
>>
>> 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.
>>
>> PatternLayout">
>>   <conversionPattern value="%custom1 %custom2 %custom3 %message%newline"
>> />
>> </layout>
>>
>>
>> Thanks,
>>
>> Bill
>>
>> =============================================
>> 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.
>>
>
>
>
>
>

Re: custom fields?

Posted by Bill McCormick <wp...@gmail.com>.
Thanks, so much Francine!!

If I understand correctly, once the GC property is set, it would stay set
for every logged message thereafter? It would actually be better if it would
be reset, since I would need (potentially) new values with each new logged
message.

Thanks,


Bill

On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor <
Francine.Taylor@genesis-fs.com> wrote:

>  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
> ActiveProperties.cs)
>
> // 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 SampleProperty();
>
> //
>
> // Then in the log4net.xml file, a pattern might contain this reference:
> %property{sampler}
>
> //
>
> 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?
>
>
>
> Hello,
>
>
> 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.
>
> PatternLayout">
>   <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
> </layout>
>
>
> Thanks,
>
> Bill
>
> =============================================
> 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.
>

Re: custom fields?

Posted by Bill McCormick <wp...@gmail.com>.
No, you got it. I'd also need to make a separate wrapper class for for the
Log.xFormat statements.

Thanks.

On Thu, Oct 2, 2008 at 1:49 PM, Francine Taylor <
Francine.Taylor@genesis-fs.com> wrote:

>  That's why the dynamic setting is so useful.  If you were just going to
> set it to a value that doesn't change, you would do it this way:
>
> log4net.GlobalContext.Properties["custom1"] = "custom1 value";
>
> But in the example that I gave, you are setting it not to a value, but to a
> class.  The ToString() value of the class, can be set dynamically, is what
> log4net uses to put the values into the %property.
>
>
>
> So you can change those values at any time.  Using the class I gave you
> below, you might put this code in your logging method
>
>
>
> public static void MyInfoLoggingMethod(string custom1, string custom2,
> string custom3, string message) {
>
>     ActiveProperties.sampleProperty = custom1;
>
>     ActiveProperties.sampleProperty = custom2;
>
>     ActiveProperties.sampleProperty = custom3;
>
>
>
>     _Log.Info(message);
>
> }
>
>
>
> Then when you log your message, those new values would be reflected.
>
>
>
> Or am I not understanding what you need?
>
>
>  ------------------------------
>
> *From:* wpmccormick@gmail.com [mailto:wpmccormick@gmail.com] *On Behalf Of
> *Bill McCormick
> *Sent:* Thursday, October 02, 2008 11:31 AM
> *To:* Log4NET User
> *Subject:* Re: custom fields?
>
>
>
> Thanks, so much Francine!!
>
> If I understand correctly, once the GC property is set, it would stay set
> for every logged message thereafter? It would actually be better if it would
> be reset, since I would need (potentially) new values with each new logged
> message.
>
> Thanks,
>
>
> Bill
>
> On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor <
> Francine.Taylor@genesis-fs.com> wrote:
>
>  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
> ActiveProperties.cs)
>
> // 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 SampleProperty();
>
> //
>
> // Then in the log4net.xml file, a pattern might contain this reference:
> %property{sampler}
>
> //
>
> 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?
>
>
>
> Hello,
>
>
>
> 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.
>
> PatternLayout">
>   <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
> </layout>
>
>
> Thanks,
>
> Bill
>
> =============================================
> 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.
>
>
>
> =============================================
> 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.
>



-- 
ACE-CO
5355 Dietrich Rd.
San Antonio, TX 78219
(210) 661-4111 x512
(800) 676-4111 Toll Free
(210) 662-6177 Fax
(210) 216-6641 Cell
mailto:wpmccormick@ace-co.com
http://www.ace-co.com

RE: custom fields?

Posted by Francine Taylor <Fr...@genesis-fs.com>.
Oops, sorry, didn't quite convert everything completely from what I've
got in my notes.  Should have been...

 

public static void MyInfoLoggingMethod(string custom1, string custom2,
string custom3, string message) {

    ActiveProperties.custom1 = custom1;

    ActiveProperties.custom2 = custom2;

    ActiveProperties.custom3 = custom3;

 

    _Log.Info(message);

}

 

 

________________________________

From: Francine Taylor [mailto:Francine.Taylor@genesis-fs.com] 
Sent: Thursday, October 02, 2008 11:49 AM
To: Log4NET User
Subject: RE: custom fields?

 

That's why the dynamic setting is so useful.  If you were just going to
set it to a value that doesn't change, you would do it this way:

log4net.GlobalContext.Properties["custom1"] = "custom1 value";

But in the example that I gave, you are setting it not to a value, but
to a class.  The ToString() value of the class, can be set dynamically,
is what log4net uses to put the values into the %property.

 

So you can change those values at any time.  Using the class I gave you
below, you might put this code in your logging method

 

public static void MyInfoLoggingMethod(string custom1, string custom2,
string custom3, string message) {

    ActiveProperties.sampleProperty = custom1;

    ActiveProperties.sampleProperty = custom2;

    ActiveProperties.sampleProperty = custom3;

 

    _Log.Info(message);

}

 

Then when you log your message, those new values would be reflected.

 

Or am I not understanding what you need?

 

________________________________

From: wpmccormick@gmail.com [mailto:wpmccormick@gmail.com] On Behalf Of
Bill McCormick
Sent: Thursday, October 02, 2008 11:31 AM
To: Log4NET User
Subject: Re: custom fields?

 

Thanks, so much Francine!!

If I understand correctly, once the GC property is set, it would stay
set for every logged message thereafter? It would actually be better if
it would be reset, since I would need (potentially) new values with each
new logged message. 

Thanks,


Bill

On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor
<Fr...@genesis-fs.com> wrote:

	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
ActiveProperties.cs)

	// 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
SampleProperty();

	//

	// Then in the log4net.xml file, a pattern might contain this
reference:  %property{sampler}

	//

	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?

	 

	Hello,

	
	
	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.

	PatternLayout">
	  <conversionPattern value="%custom1 %custom2 %custom3
%message%newline" />
	</layout>
	
	
	Thanks,
	
	Bill

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

 

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

 


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

RE: custom fields?

Posted by Francine Taylor <Fr...@genesis-fs.com>.
That's why the dynamic setting is so useful.  If you were just going to
set it to a value that doesn't change, you would do it this way:

log4net.GlobalContext.Properties["custom1"] = "custom1 value";

But in the example that I gave, you are setting it not to a value, but
to a class.  The ToString() value of the class, can be set dynamically,
is what log4net uses to put the values into the %property.

 

So you can change those values at any time.  Using the class I gave you
below, you might put this code in your logging method

 

public static void MyInfoLoggingMethod(string custom1, string custom2,
string custom3, string message) {

    ActiveProperties.sampleProperty = custom1;

    ActiveProperties.sampleProperty = custom2;

    ActiveProperties.sampleProperty = custom3;

 

    _Log.Info(message);

}

 

Then when you log your message, those new values would be reflected.

 

Or am I not understanding what you need?

 

________________________________

From: wpmccormick@gmail.com [mailto:wpmccormick@gmail.com] On Behalf Of
Bill McCormick
Sent: Thursday, October 02, 2008 11:31 AM
To: Log4NET User
Subject: Re: custom fields?

 

Thanks, so much Francine!!

If I understand correctly, once the GC property is set, it would stay
set for every logged message thereafter? It would actually be better if
it would be reset, since I would need (potentially) new values with each
new logged message. 

Thanks,


Bill

On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor
<Fr...@genesis-fs.com> wrote:

	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
ActiveProperties.cs)

	// 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
SampleProperty();

	//

	// Then in the log4net.xml file, a pattern might contain this
reference:  %property{sampler}

	//

	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?

	 

	Hello,

	
	
	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.

	PatternLayout">
	  <conversionPattern value="%custom1 %custom2 %custom3
%message%newline" />
	</layout>
	
	
	Thanks,
	
	Bill

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

 


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

Re: custom fields?

Posted by Bill McCormick <wp...@ace-co.com>.
Thanks, so much Francine!!

If I understand correctly, once the GC property is set, it would stay set
for every logged message thereafter? It would actually be better if it would
be reset, since I would need (potentially) new values with each new logged
message.

Thanks,


Bill

On Thu, Oct 2, 2008 at 12:01 PM, Francine Taylor <
Francine.Taylor@genesis-fs.com> wrote:

>  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
> ActiveProperties.cs)
>
> // 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 SampleProperty();
>
> //
>
> // Then in the log4net.xml file, a pattern might contain this reference:
> %property{sampler}
>
> //
>
> 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?
>
>
>
> Hello,
>
>
> 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.
>
> PatternLayout">
>   <conversionPattern value="%custom1 %custom2 %custom3 %message%newline" />
> </layout>
>
>
> Thanks,
>
> Bill
>
> =============================================
> 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.
>

RE: custom fields?

Posted by Francine Taylor <Fr...@genesis-fs.com>.
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
ActiveProperties.cs)

// 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
SampleProperty();

//

// Then in the log4net.xml file, a pattern might contain this reference:
%property{sampler}

//

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?

 

Hello,

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.

PatternLayout">
  <conversionPattern value="%custom1 %custom2 %custom3 %message%newline"
/>
</layout>


Thanks,

Bill


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