views:

274

answers:

2

I am attempting to provide a means to log errors that occur in our Flex client by providing a SOAP web service which takes as a single parameter a LogMessage object.

public class LogMessage
{
    public string Message { get; set; }
    public string Exception { get; set; }
    public string Version { get; set; }
    public string User { get; set; }
}

This object is populated by the Flex client should a client side error surface and the LogClientError method is invoked which logs the error via log4net.

[WebMethod()]
public void LogClientError(LogMessage message) 
{
    rollingLogger.Error(message);
}

Currently this prints the fully qualified name of the LogMessage class, so my current assumption is that log4net simply calls .ToString() on the object that is passed in.

However, what I really want to be able to do is map each property in the LogMessage class to a pattern so that log4net will correctly write out the desired information. I would like to do this in such a manner that the typical appenders (DB, File, SMTP) are all still supported.

How do I configure log4net such that properties of an object can be mapped to the log output?

+2  A: 

log4net has two paths you can take. The first would be to create a custom object renderer for LogMessage instances. You need to implement the IObjectRenderer interface and register the implementation.

The other route, which would be more reusable, is to subclass the patternlayout class. Your custom pattern layout could then support a special syntax for naming properties which the layout could use to reflect on the incoming message object.

Peter Lillevold
A: 

If LogMessage is partial you could create a ToString() method on LogMessage.

Carl Bergquist