OK,
So far I've created the following 2 very simple classes which appear to give
me what I need:
class EventPropertiesXMLLayout : XmlLayoutBase
{
protected override void FormatXml(XmlWriter writer, LoggingEvent
loggingEvent)
{
LogMessage lm = loggingEvent.MessageObject as LogMessage;
if (lm != null)
{
writer.WriteStartElement("LogEntry");
writer.WriteStartElement("Message");
writer.WriteValue(lm.Message);
writer.WriteEndElement();
writer.WriteStartElement("Properties");
foreach (DictionaryEntry de in lm.Properties)
{
writer.WriteStartElement(de.Key.ToString());
writer.WriteValue(de.Value.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
class LogMessage
{
private string _message;
private Hashtable _properties;
public LogMessage(string message)
{
_message = message;
_properties = new Hashtable();
}
public string Message
{
get
{
return _message;
}
}
public Hashtable Properties
{
get
{
return _properties;
}
}
}
And then write the following code if I have to collect custom properties:
LogMessage lm = new LogMessage("This is a custom log message");
lm.Properties.Add("prop1", "value1");
lm.Properties.Add("prop2", "value2");
log.Info(lm);
Can you see any immediate issues or limitations with this approach?
AdamTappis wrote:
>
> How about if I create a custom LogMessage Class that has the message and
> distionary as properties and then write a cutom ObjectRenderer for my
> custom class. This would give my developers the flexibility of either
> logging strings or the custom LogMessage if they require additional
> properties. I guess what it doesn't give me is the flexibility or
> rendering the object to a file using one format and to a DB using a
> different formats. Objects of the specified type would always be rendered
> the same way.
>
>
> AdamTappis wrote:
>>
>> Having a closer look at the documentation, the Properties property of the
>> LoggingEvent class is a PropertiesDictionary which derives from
>> ReadOnlyPropertiesDictionary which menas it can't be written to and hence
>> it's not suitable for my purposes.
>>
>> Any ideas?
>>
>>
>> AdamTappis wrote:
>>>
>>> Ross,
>>>
>>> I guess that's why I'm posting here, because I'm trying to devise a
>>> pattern and some coding guidelines so that all our applications log
>>> consistently. I considered wrapping log4net but all I've read advises
>>> against that. So what I'm trying to achieve is an elegant solution that
>>> makes the best use of the framework.
>>>
>>> You're right, I wish to log a collection of Key-Value pairs associated
>>> with a given loggin event. e.g.
>>> statistics -
>>> <eventData><duration>3ms</duration><size>10kb</size></eventData>
>>> details -
>>> <eventData><customerID>3</customerID><orderID>10</orderID></eventData>
>>>
>>> The question I'm asking is how best to implement this with log4net?
>>>
>>> Should I write a wrapper or a helper class that accepts the key-Value
>>> pairs collection as a parameter and returns a formattred XML string that
>>> then get's logged?
>>>
>>> Maybe I should be creating a LoggingEvent in code manually, adding the
>>> Key-Value pairs to the Properties collection and then using
>>> ILog.Logger.Log to actually log the event rather than using Ilog.Info()
>>> to log. I could than write a custom layout class that serialises the
>>> properties out as an XML string.
>>>
>>> like I said, I'm not sure and am looking for some disrection.
>>>
>>> Ideally I would have the following interface available to me:
>>>
>>> ILog.Info(string message, Dictionary customProperties)
>>>
>>> and then it would be the configuration that drives how the
>>> customProperties are rendered by specifying the approporiate Layout
>>> class. That way I could format the customProperties to a file in one way
>>> and to a Db as XML etc... without embedding the formatting logic in my
>>> code.
>>>
>>>
>>> Ross Hinkley wrote:
>>>>
>>>> Adam,
>>>>
>>>> How are these properties getting transformed from a logging call to
>>>> XML?
>>>> Are you asking if there's a way to modify a custom format on the fly
>>>> and
>>>> then turn the custom properties into XML? (I'm thinking of something
>>>> like
>>>> appending name-value pairs to the log format and having a custom
>>>> appender do
>>>> the XML conversion for you. This doesn't seem terribly elegant.)
>>>>
>>>> Maybe I'm over-trivializing, but it seems to me you should be able to
>>>> use
>>>> custom properties to take care of your current needs, converting your
>>>> properties to XML in code, then modifying the ADO appender
>>>> configuration to
>>>> handle that property appropriately when the logging database is
>>>> implemented.
>>>>
>>>> I guess another question would be what sorts of things need to be
>>>> serialized? Are they going to be serialized from a class?
>>>>
>>>> -Ross
>>>>
>>>> On Mon, Aug 17, 2009 at 8:12 AM, AdamTappis <[email protected]>
>>>> wrote:
>>>>
>>>>>
>>>>> Hi,
>>>>>
>>>>> I've been evaluating log4net recently with a view to using the
>>>>> framework as
>>>>> a loggin standard for our enterprise applicaption. I see it very easy
>>>>> to
>>>>> extend the framework, however I have some specific logging
>>>>> requirements and
>>>>> I'm not sure what would be the easiest way implement these with
>>>>> minimal
>>>>> coding.
>>>>>
>>>>> At some time in the future, we aim to build a consolidated logging
>>>>> database
>>>>> that will capture logging data from our server application suite. The
>>>>> information that needs to be logged will differ per
>>>>> service/applicaption
>>>>> and
>>>>> to that end I would aim to have a table schema that has the standard
>>>>> logging
>>>>> columns plus a generic XML column (no defined schema) to act as a
>>>>> property
>>>>> bag for custom information associated with a specific event.
>>>>>
>>>>> We don't have time to develop the logging repository at present,
>>>>> however
>>>>> I'm
>>>>> stressing that we should build logging into our code from the start
>>>>> rather
>>>>> than trying to retro-fitting it later which would be far more costly.
>>>>>
>>>>> So I need my code to be able to fire off logging messages with a list
>>>>> or
>>>>> arbitrary custom properties that get formatted into an XML string. For
>>>>> now
>>>>> the messages can be written to a text file and later we'll configure
>>>>> and
>>>>> AdoNetAppender to write to out logging database.
>>>>>
>>>>> So my questions are:
>>>>> - Is this possible using configuration only? (I susopect not)
>>>>> - If not then which custom objects do I need to create? From what I've
>>>>> seen
>>>>> so far I think I'll need to code my own Layout Class
>>>>> - The ILog interface doesn't doesn't expose a method that takes a
>>>>> property
>>>>> bag (or dictionary), but it appears that the LoggingEvent object does.
>>>>> Does
>>>>> that mean I would have to make my logging calls using
>>>>> ILog.Logger.Log()?
>>>>> - Could someone provide some sample code please?
>>>>> --
>>>>> View this message in context:
>>>>> http://www.nabble.com/Adding-custom-logging-properties-tp25006538p25006538.html
>>>>> Sent from the Log4net - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Adding-custom-logging-properties-tp25006538p25011405.html
Sent from the Log4net - Users mailing list archive at Nabble.com.