logging-log4net-user mailing list archives

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